Python Sets

                                Sets​

An unordered collection of unique and mutable objects that supports operations corresponding to mathematical set theory​
Set is mutable​.


No duplicates
Sets are iterable, can grow and shrink on demand, and may contain a variety of object types​
Does not support indexing​


EXAMPLE 1:

x = {1, 2, 3, 4}​
y = {'apple','ball','cat'}​
x1 = set('spam')  # Prepare set from a string
print (x1)​
{'s', 'a', 'p', 'm'}​
x1.add('alot')    # Add an element to the set ​
print (x1)​
{'s', 'a', 'p', 'alot', 'm'}​


EXAMPLE 2:

 Set Operations​

Let S1 = {1, 2, 3, 4}​
Union (|)​
S2 = {1, 5, 3, 6} | S1​
print(S2) # prints {1, 2, 3, 4, 5, 6}​
Intersection (&)​
S2 = S1 & {1, 3}​
print(S2) # prints {1, 3}​


EXAMPLE 3:

Difference (-)​

S2 = S1 - {1, 3, 4}​
print(S2) # prints {2}​
Super set (>)​
S2 = S1 > {1, 3}​
print(S2) # prints True​
Empty sets must be created with the set built-in, and print the same way​


EXAMPLE 4:

S2 = S1 - {1, 2, 3, 4}​
print(S2) # prints set() – Empty set​
Empty curly braces represent empty dictionary but not set​
In interactive mode – type({}) gives​
<class 'dict'>​


EXAMPLE 5:

>>> {1, 2, 3} | {3, 4}​
{1, 2, 3, 4}​
>>> {1, 2, 3} | [3, 4]​
TypeError: unsupported operand type(s) for |: 'set' and 'list'​
>>> {1, 2, 3} | set([3, 4]) #Convert list to set and work​
{1,2,3,4}​


EXAMPLE 6:

>>> {1, 2, 3}.union([3, 4])​
{1,2,3,4}​
>>> {1, 2, 3}.union({3, 4})​
{1,2,3,4}​


Immutable constraints and frozen sets​

Can only contain immutable (a.k.a. “hashable”) object types​
lists and dictionaries cannot be embedded in sets, but tuples can if you need to store compound values.​
Tuples compare by their full values when used in set operations:​
>>> S​
{1.23}​
>>> S.add([1, 2, 3])​
TypeError: unhashable type: 'list'​


EXAMPLE 1:

>>> S.add({'a':1})​
TypeError: unhashable type: 'dict'​
Works for tuples:​
>>> S.add((1, 2, 3))​
>>> S​
{1.23, (1, 2, 3)}​​

EXAMPLE 2:

>>> S | {(4, 5, 6), (1, 2, 3)}​
{1.23, (4, 5, 6), (1, 2, 3)}​
>>> (1, 2, 3) in S # Check for tuple as a whole​
True​
>>> (1, 4, 3) in S​
False​

EXAMPLE 3:

clear()​

All elements will removed from a set.​
>>> cities = {"Stuttgart", "Konstanz", "Freiburg"}​
>>> cities.clear()​
>>> cities​
set() # empty​
>>> ​

EXAMPLE 4:

Copy

Creates a shallow copy, which is returned.​
>>> more_cities = {"Winterthur","Schaffhausen","St. Gallen"}​
>>> cities_backup = more_cities.copy()​
>>> more_cities.clear()​
>>> cities_backup  # copied value is still available​
{'St. Gallen', 'Winterthur', 'Schaffhausen‘}​

EXAMPLE 5:

Just in case, you might think, an assignment might be enough:​
>>> more_cities = {"Winterthur","Schaffhausen","St. Gallen"}​
>>> cities_backup = more_cities #creates aliasname​
>>> more_cities.clear()​
>>> cities_backup​
set()​
>>> ​
The assignment "cities_backup = more_cities" just creates a pointer, i.e. another name, to the same data structure. ​

EXAMPLE 6:

difference_update()​

removes all elements of another set from this set. x.difference_update() is the same as "x = x - y" ​
>>> x = {"a","b","c","d","e"}​
>>> y = {"b","c"}​
>>> x.difference_update(y)​
>>> x​
{'a', 'e', 'd'}​

Blog

Post a Comment

Previous Post Next Post