Set
.. Set([itr])
Construct a :obj:`Set` of the values generated by the given iterable object, or an empty set.
Should be used instead of :obj:`IntSet` for sparse integer sets, or for sets of arbitrary objects.
Examples
In the Julia programming language, the function Set([itr])
is used to construct a Set
of values generated by the given iterable object, or an empty set if no iterable object is provided. It is recommended to use Set
instead of IntSet
for sparse integer sets or sets of arbitrary objects.
Examples of how to use the Set
function:
-
Create a set from an iterable:
julia> set1 = Set([1, 2, 3, 4, 5]) Set{Int64} with 5 elements: 4 2 3 5 1
This example creates a set
set1
with the elements from the iterable[1, 2, 3, 4, 5]
. -
Create an empty set:
julia> set2 = Set() Set{Any} with 0 elements
This example creates an empty set
set2
of typeSet{Any}
. - Create a set of strings:
julia> set3 = Set(["apple", "banana", "orange"]) Set{String} with 3 elements: "banana" "orange" "apple"
It creates a set
set3
with the elements as strings.
Common mistake example:
julia> set4 = Set(1:5)
ERROR: MethodError: no method matching Set(::UnitRange{Int64})
In this example, the provided iterable is not enclosed in square brackets. To create a set from an iterable, always wrap the iterable inside square brackets, like Set([iterable])
.
See Also
complement, complement!, intersect, intersect!, issubset, selectperm, selectperm!, Set, setdiff, setdiff!, symdiff, union, union!,User Contributed Notes
Add a Note
The format of note supported is markdown, use triple backtick to start and end a code block.