Sets
Julia has a datatype called Set
, that is mathematical equivalent of a Set. From the angle of Julia you can think Set
as an Array
which contains just unique values and cannot be ordered or sorted.
So let’s dive in, we can create a set as shown below:
set = Set([1, 2, 3, 4])
Output:
Set{Int64} with 4 elements:
4
2
3
1
So we see that we use an array [1, 2, 3, 4]
, this is been passed to a Function called Set()
like this Set([1, 2, 3, 4])
and out we get out a set which is stored in a variable called set
. Why don’t you try out Set([1, 2, 3, 4, 1, 2, 3, 4])
and see what happens?
We will also create another set as shown:
another_set = Set((3, 4, 5, 6, 7))
Output:
Set{Int64} with 5 elements:
7
4
3
5
6
Now we will try to push in a new value 8
in another set. Note we use push!()
function which means the argument passed to it gets modified:
push!(another_set, 8)
Output:
Set{Int64} with 6 elements:
7
4
3
5
8
6
In the above piece of code we give another_set
as argument to the push!()
and we add 8 to it, so another_set
get modified.
Let’s now try to push 8
again to another_set
:
push!(another_set, 8)
Output:
Set{Int64} with 6 elements:
7
4
3
5
8
6
As you can see from the above example, a set contains just unique values and if you push a value that already exists, it will not grow.
Unions
Julia has a union()
function with which you could get a set’s union as shown:
union(set, another_set)
Output:
Set{Int64} with 8 elements:
7
4
2
3
5
8
6
1
Intersection
Julia has a intersect()
function with which you can find intersection of two sets as shown:
intersect(set, another_set)
Output:
Set{Int64} with 2 elements:
4
3
Difference
Julia has setdiff()
function with which you can find the difference of two sets as shown, in the belowcode it means get all the elements of set
that are not there in another_set
:
setdiff(set, another_set)
Output:
Set{Int64} with 2 elements:
2
1
Other Operations
You can find number of elements in a set using length()
function as shown:
length(set)
Output:
4
To get the type of set you can use the typeof()
operator as shown:
typeof(set)
Output:
Set{Int64}
Set is just another collection so you can use for
loop as shown:
for element in set
print(element, ", ")
end
Output:
4, 2, 3, 1,
Rather than using the cumbersome for
, you can print the values of set as shown:
print(join(set, ", "))
Output:
4, 2, 3, 1
In the above example join()
, joins the set
elements with a string ", "
and outputs this string: "4, 2, 3, 1"
, this when passed to println()
outputs 4, 2, 3, 1
.
You can’t sort a Set
You can’t sort a set, unlike Array
and Tuple
a set is unordered:
sort(set)
Output:
MethodError: no method matching sort(::Set{Int64})
Closest candidates are:
sort(!Matched::AbstractUnitRange) at range.jl:1014
sort(!Matched::AbstractRange) at range.jl:1017
sort(!Matched::SparseArrays.SparseVector{Tv,Ti}; kws...) where {Tv, Ti} at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.5/SparseArrays/src/sparsevector.jl:1912
...
Stacktrace:
[1] top-level scope at In[12]:1
[2] include_string(::Function, ::Module, ::String, ::String) at ./loading.jl:1091
[3] execute_code(::String, ::String) at /home/karthikeyan/.julia/packages/IJulia/a1SNk/src/execute_request.jl:27
[4] execute_request(::ZMQ.Socket, ::IJulia.Msg) at /home/karthikeyan/.julia/packages/IJulia/a1SNk/src/execute_request.jl:86
[5] #invokelatest#1 at ./essentials.jl:710 [inlined]
[6] invokelatest at ./essentials.jl:709 [inlined]
[7] eventloop(::ZMQ.Socket) at /home/karthikeyan/.julia/packages/IJulia/a1SNk/src/eventloop.jl:8
[8] (::IJulia.var"#15#18")() at ./task.jl:356
Converting Set to Array
You can’t convert a set to array as easy as passing a set to an Array()
as shown:
Array(set)
Output:
MethodError: no method matching Array(::Set{Int64})
Closest candidates are:
Array(!Matched::LinearAlgebra.SymTridiagonal) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.5/LinearAlgebra/src/tridiag.jl:142
Array(!Matched::LinearAlgebra.Tridiagonal) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.5/LinearAlgebra/src/tridiag.jl:583
Array(!Matched::LinearAlgebra.AbstractTriangular) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.5/LinearAlgebra/src/triangular.jl:157
...
Stacktrace:
[1] top-level scope at In[13]:1
[2] include_string(::Function, ::Module, ::String, ::String) at ./loading.jl:1091
[3] execute_code(::String, ::String) at /home/karthikeyan/.julia/packages/IJulia/a1SNk/src/execute_request.jl:27
[4] execute_request(::ZMQ.Socket, ::IJulia.Msg) at /home/karthikeyan/.julia/packages/IJulia/a1SNk/src/execute_request.jl:86
[5] #invokelatest#1 at ./essentials.jl:710 [inlined]
[6] invokelatest at ./essentials.jl:709 [inlined]
[7] eventloop(::ZMQ.Socket) at /home/karthikeyan/.julia/packages/IJulia/a1SNk/src/eventloop.jl:8
[8] (::IJulia.var"#15#18")() at ./task.jl:356
You need to use list comprehension trickery as shown:
array_from_set = [element for element in set]
Output:
4-element Array{Int64,1}:
4
2
3
1
As though we do not believe we can make Array
from Set
we check the typeof of array_from_set
typeof(array_from_set)
Output:
Array{Int64,1}
Coverting Set to Tuple
We do not get an Array
from set using the Array()
thing, but we can get a Tuple
, using Tuple()
as shown:
Tuple(set)
Output:
(4, 2, 3, 1)
That’s sad, because Array
is treated as unwanted second class citizens, but may be there could be a reason for that which I am unaware.
Pop out a element from a Set
We can pop out a element from a set using the pop!()
function as shown:
pop!(set)
Output:
4
Note that since we have used bang !
, it modifies the passed variable set
and the popped out value 4
get removed from set
as shown below:
set
Output:
Set{Int64} with 3 elements:
2
3
1
size()
returns a Tuple
and its for multidimensional data structure like Array
, so it won’t work on Set
:
size(set)
Output:
MethodError: no method matching size(::Set{Int64})
Closest candidates are:
size(!Matched::BitArray{1}) at bitarray.jl:104
size(!Matched::BitArray{1}, !Matched::Integer) at bitarray.jl:107
size(!Matched::Core.Compiler.StmtRange) at show.jl:1874
...
Stacktrace:
[1] top-level scope at In[19]:1
[2] include_string(::Function, ::Module, ::String, ::String) at ./loading.jl:1091
[3] execute_code(::String, ::String) at /home/karthikeyan/.julia/packages/IJulia/a1SNk/src/execute_request.jl:27
[4] execute_request(::ZMQ.Socket, ::IJulia.Msg) at /home/karthikeyan/.julia/packages/IJulia/a1SNk/src/execute_request.jl:86
[5] #invokelatest#1 at ./essentials.jl:710 [inlined]
[6] invokelatest at ./essentials.jl:709 [inlined]
[7] eventloop(::ZMQ.Socket) at /home/karthikeyan/.julia/packages/IJulia/a1SNk/src/eventloop.jl:8
[8] (::IJulia.var"#15#18")() at ./task.jl:356
Get the Notebook
Well that’s it for sets, get the Jpyter notebook for it here https://gitlab.com/data-science-with-julia/code/-/blob/master/sets.ipynb