Python Language Set: Difference between revisions
(Created page with "=Internal= * Python Language") |
|||
(26 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
=Internal= | =Internal= | ||
* [[Python_Language#Set|Python Language]] | * [[Python_Language#Set|Python Language]] | ||
=Overview= | |||
A set is an unordered collection of unique elements. Like dictionary keys, set elements must be immutable and [[Eq_()_and_hash_()_in_Python#Hashability|hashable]]. | |||
=Sets and the <tt>__hash__()</tt> Function= | |||
{{Internal|Eq_()_and_hash_()_in_Python#Dictionaries_and_the_hash_.28.29_Function|Sets and the <code>__hash__()</code> Function}} | |||
=<span id='Initialization'></span>Creation and Initialization= | |||
A set can be created in two ways, via the <code>[[#set()|set()]]</code> function or with a [[#Set_Literal|set literal]]. | |||
<span id='Set_Literal'></span>A set can be created and initialization with a curly braces set literal: | |||
<syntaxhighlight lang='py'> | |||
s = {1, 2, 3} | |||
</syntaxhighlight> | |||
<span id='set()'></span>Sets can also be created with the <code>set()</code> function: | |||
Set from a tuple: | |||
<syntaxhighlight lang='py'> | |||
s = set((1, 2, 3)) | |||
</syntaxhighlight> | |||
Set from a list: | |||
<syntaxhighlight lang='py'> | |||
s = set([1, 2, 3]) | |||
</syntaxhighlight> | |||
Set from a string: | |||
<syntaxhighlight lang='py'> | |||
s = set('ABC') | |||
print(s) # {'C', 'B', 'A'} | |||
</syntaxhighlight> | |||
=Set Equality= | |||
Sets are equal only if their contents are equal: | |||
<syntaxhighlight lang='py'> | |||
assert {1, 2, 3} == {3, 2, 1} | |||
</syntaxhighlight> | |||
=Shallow Copy= | |||
<syntaxhighlight lang='py'> | |||
s = set() | |||
s.add('a') | |||
s2 = s.copy() | |||
</syntaxhighlight> | |||
=Add Elements= | |||
==Add One Element== | |||
<syntaxhighlight lang='py'> | |||
s = set() | |||
s.add('a') | |||
assert 'a' in s | |||
</syntaxhighlight> | |||
==Add Multiple Elements== | |||
Update the set with the union of itself and others, where others can be any iterable. <code>update()</code> eliminates duplicates. | |||
<syntaxhighlight lang='py'> | |||
s = set() | |||
s.update(['a', 'b', 'a']) | |||
assert len(s) == 2 | |||
assert 'a' in s | |||
assert 'b' in s | |||
</syntaxhighlight> | |||
==Add Multiple Elements with <tt>union()</tt>== | |||
<tt>union()</tt> merges the elements of the iterable given as argument into the current set, and returns a new set, which is the union of the current set and the argument. | |||
<syntaxhighlight lang='py'> | |||
a = {1, 2} | |||
b = {5, 6} | |||
assert a | b == {1, 2, 5, 6} | |||
assert a.union(b) == {1, 2, 5, 6} | |||
</syntaxhighlight> | |||
=Remove an Element from a Set= | |||
<syntaxhighlight lang='py'> | |||
s.remove('a') | |||
</syntaxhighlight> | |||
Does not return anything, simply removes the element. | |||
If the element does not exist, throws <code>KeyError: 'a'</code>. | |||
<syntaxhighlight lang='py'> | |||
s.pop() | |||
</syntaxhighlight> | |||
Remove an arbitrary element from the set, raising <code>KeyError</code> if the set is empty. | |||
=Convert to List= | |||
<syntaxhighlight lang='py'> | |||
s=set() | |||
l=list(s) | |||
</syntaxhighlight> | |||
=Set Operations= | |||
==Union== | |||
See [[#Add_Multiple_Elements_with_union()|above]]. | |||
==Inclusion== | |||
Check whether a set is included in another set with <code>issubset()</code>: | |||
<syntaxhighlight lang='py'> | |||
set_a = {'a', 'b', 'c', 'd'} | |||
set_b = {'a', 'c'} | |||
assert set_b.issubset(set_a) | |||
</syntaxhighlight> | |||
The function also works for non-homogenous sets, where the elements have different types: | |||
<syntaxhighlight lang='py'> | |||
set_a = {'a', 'b', 'c', 1, 5, 9} | |||
set_b = {1, 'c'} | |||
assert set_b.issubset(set_a) | |||
</syntaxhighlight> | |||
==Intersection== | |||
<syntaxhighlight lang='py'> | |||
a = {'a', 'b', 'c', 1, 2, 3} | |||
b = {'x', 'y', 'c', 1, 10, 20} | |||
assert a.intersection(b) == {1, 'c'} | |||
assert a & b == {1, 'c'} | |||
</syntaxhighlight> | |||
=Clear= | |||
Reset the set to an empty state, discarding all elements: | |||
<syntaxhighlight lang='py'> | |||
a.clear() | |||
</syntaxhighlight> | |||
=<tt>frozenset()</tt>= | |||
The <code>[[Python Language Functions#frozenset|fronzenset()]]</code> returns an immutable frozenset object initialized with the elements from a given iterable. frozenset objects can be used as dictionary keys. | |||
If the iterable contains duplicate elements, they are ignored (the iterable is handled like a set): | |||
<syntaxhighlight lang='py'> | |||
f = frozenset(['a', 'b', 'c']) | |||
f2 = frozenset(['a', 'b', 'c', 'a']) | |||
assert f == f2 | |||
</syntaxhighlight> |
Latest revision as of 17:12, 17 May 2024
Internal
Overview
A set is an unordered collection of unique elements. Like dictionary keys, set elements must be immutable and hashable.
Sets and the __hash__() Function
Creation and Initialization
A set can be created in two ways, via the set()
function or with a set literal.
A set can be created and initialization with a curly braces set literal:
s = {1, 2, 3}
Sets can also be created with the set()
function:
Set from a tuple:
s = set((1, 2, 3))
Set from a list:
s = set([1, 2, 3])
Set from a string:
s = set('ABC')
print(s) # {'C', 'B', 'A'}
Set Equality
Sets are equal only if their contents are equal:
assert {1, 2, 3} == {3, 2, 1}
Shallow Copy
s = set()
s.add('a')
s2 = s.copy()
Add Elements
Add One Element
s = set()
s.add('a')
assert 'a' in s
Add Multiple Elements
Update the set with the union of itself and others, where others can be any iterable. update()
eliminates duplicates.
s = set()
s.update(['a', 'b', 'a'])
assert len(s) == 2
assert 'a' in s
assert 'b' in s
Add Multiple Elements with union()
union() merges the elements of the iterable given as argument into the current set, and returns a new set, which is the union of the current set and the argument.
a = {1, 2}
b = {5, 6}
assert a | b == {1, 2, 5, 6}
assert a.union(b) == {1, 2, 5, 6}
Remove an Element from a Set
s.remove('a')
Does not return anything, simply removes the element.
If the element does not exist, throws KeyError: 'a'
.
s.pop()
Remove an arbitrary element from the set, raising KeyError
if the set is empty.
Convert to List
s=set()
l=list(s)
Set Operations
Union
See above.
Inclusion
Check whether a set is included in another set with issubset()
:
set_a = {'a', 'b', 'c', 'd'}
set_b = {'a', 'c'}
assert set_b.issubset(set_a)
The function also works for non-homogenous sets, where the elements have different types:
set_a = {'a', 'b', 'c', 1, 5, 9}
set_b = {1, 'c'}
assert set_b.issubset(set_a)
Intersection
a = {'a', 'b', 'c', 1, 2, 3}
b = {'x', 'y', 'c', 1, 10, 20}
assert a.intersection(b) == {1, 'c'}
assert a & b == {1, 'c'}
Clear
Reset the set to an empty state, discarding all elements:
a.clear()
frozenset()
The fronzenset()
returns an immutable frozenset object initialized with the elements from a given iterable. frozenset objects can be used as dictionary keys.
If the iterable contains duplicate elements, they are ignored (the iterable is handled like a set):
f = frozenset(['a', 'b', 'c'])
f2 = frozenset(['a', 'b', 'c', 'a'])
assert f == f2