Python Concepts/Sets
Objective
[edit | edit source]
|
Lesson
[edit | edit source]
Python Sets[edit | edit source]Sets are mutable sequences, like lists. However, sets and lists differ. Unlike lists, you cannot use >>> spam = {1, 2, 3}
>>> spam
{1, 2, 3}
>>> eggs = {1, 2, 1, 3, 5, 2, 7, 3, 4}
>>> eggs
{1, 2, 3, 4, 5, 7} # each object unique
>>> {True, False, True, False, True}
{False, True}
>>> {"hi", "hello", "hey", "hi", "hiya", "sup"}
{'hey', 'sup', 'hi', 'hello', 'hiya'}
>>>
>>> a = {} ; a
{}
>>> isinstance(a,set)
False
>>>
>>> a = set() ; a # to create empty set.
set()
>>> isinstance(a,set)
True
>>>
|
Operations on a single set
[edit | edit source]
Initialize the set:[edit | edit source]>>> b = set('alacazam') ; b
{'z', 'l', 'm', 'c', 'a'}
>>>
>>> b = {'alacazam'} ; b
{'alacazam'}
>>>
>>> b = {'pear','plum'} ; b
{'pear', 'plum'}
>>>
>>> d = ['apple', 'pear', 'plum', 'peach', 'pecan', 'plum', 'peach', 'pecan', 'plum', 'peach'] ; d
['apple', 'pear', 'plum', 'peach', 'pecan', 'plum', 'peach', 'pecan', 'plum', 'peach']
>>> b = set(d) ; b
{'peach', 'pecan', 'pear', 'plum', 'apple'}
>>>
Familiar operations:[edit | edit source]>>> isinstance(b,set)
True
>>> len(b)
5
>>> 'apple' in b
True
>>> 'grape' in b
False
>>>
>>> 'grape' not in b
True
>>>
>>> for x in b : print ( x[0:3] ) # for x in set :
...
pea
pec
pea
plu
app
>>>
>>> f = b # A shallow copy.
>>> f == b
True
>>> f is b
True
>>> f = set(b) # A deep copy.
>>> f == b
True
>>> f is b
False
>>>
Operations available for set:[edit | edit source]>>> b = set() ; b.add('alacazam') ; b # add element 'alacazam' to set b.
{'alacazam'}
>>>
>>> b = {'pear','plum'} ; b
{'pear', 'plum'}
>>>
>>> d = ['apple', 'pear', 'plum', 'peach', 'pecan'] ; d
['apple', 'pear', 'plum', 'peach', 'pecan']
>>> for c in d : b.add(c) ; b
...
{'pear', 'apple', 'plum'} # 'apple' was added
{'pear', 'apple', 'plum'} # 'pear' was not added.
{'pear', 'apple', 'plum'} # 'plum' was not added.
{'peach', 'pear', 'apple', 'plum'} # 'peach' was added
{'peach', 'pecan', 'pear', 'plum', 'apple'} # 'pecan' was added. ordering not same as list d.
>>>
>>> b = {'peach', 'pecan', 'pear', 'plum', 'apple'} ; b
{'peach', 'pecan', 'pear', 'plum', 'apple'}
>>> b.clear() ; b # remove all elements from set b
set()
>>>
>>> b = {'peach', 'pecan', 'pear', 'plum', 'apple'} ; b
{'peach', 'pecan', 'pear', 'plum', 'apple'}
>>> a = b.pop() ; a ; b # Remove and return an arbitrary element from the set. Raises KeyError if the set is empty.
'peach'
{'pecan', 'pear', 'plum', 'apple'}
>>>
>>> b.discard('grape') ; b # Remove element 'grape' from set b if element is present.
{'pecan', 'pear', 'plum', 'apple'}
>>>
>>> b.discard('pear') ; b
{'pecan', 'plum', 'apple'}
>>>
>>> b.remove('apple') ; b # Remove element 'apple' from set b. Raises KeyError if element is not contained in the set.
{'pecan', 'plum'}
>>>
Set comprehensions[edit | edit source]Similarly to list comprehensions, set comprehensions are also supported: >>> {x*x%7 for x in range(-234,79)}
{0, 1, 2, 4}
>>>
>>> a = {x for x in 'abracadabra' if x in 'abcrmgz'} ; a
{'b', 'a', 'c', 'r'}
>>>
|
Operations on two sets
[edit | edit source]
[edit | edit source] |
Operations on two or more sets
[edit | edit source]
Union[edit | edit source]
Return a new set with elements from >>> newSet = {'a', 'b', 'c', 'g'}.union( {'b', 'h'},'abcz', [1,2,3] ) ; newSet # iterable as argument
{'b', 1, 'z', 'c', 2, 'h', 3, 'g', 'a'}
>>>
>>> newSet = {'a', 'b', 'c', 'g'} | {'b', 'h'} | set([1,2,3,4]) ; newSet # operands must be sets.
{'b', 1, 2, 'c', 3, 'h', 4, 'g', 'a'}
>>>
>>> newSet = {'a', 'b', 'c', 'g'} | {'b', 'h'} | [1,2,3,4] ; newSet
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for |: 'set' and 'list'
>>>
Intersection[edit | edit source]
Return a new set with elements common to >>> newSet = {'a', 'b', 'c', 'g'}.intersection( {'g', 'b', 'h'},'abczg' ) ; newSet # iterable as argument
{'b', 'g'}
>>
>>> newSet = {'a', 'b', 'c', 'g'}.intersection( {'g', 'b', 'h'},'abczg','p', 'q', 's', 'b', 'g' ) ; newSet
set()
>>>
>>> newSet = {'a', 'b', 'c', 'g'} & {'g', 'b', 'h'} ; newSet # operands must be sets
{'b', 'g'}
>>>
>>> newSet = {'a', 'b', 'c', 'g'} & {'g', 'b', 'h'} & set(('g', 'b', 'z', 'm')) ; newSet
{'b', 'g'}
>>>
>>> newSet = {'a', 'b', 'c', 'g'} & {'g', 'b', 'h'} & ('g', 'b', 'z', 'm') ; newSet
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for &: 'set' and 'tuple'
>>>
Difference[edit | edit source]
Return a new set with elements in >>> newSet = {'a', 'b', 'c', 'g','q', 'x'}.difference( {'g', 'b', 'h'},'bczg' ) ; newSet # iterable as argument
{'q', 'x', 'a'}
>>>
>>> newSet = {'a', 'b', 'c', 'g','q', 'x'} - {'g', 'b', 'h'} - set('bczg') ; newSet # operands are sets
{'a', 'q', 'x'}
>>> newSet = {'a', 'b', 'c', 'g','q', 'x'} - set( {'g', 'b', 'h'} | set('bczg') ) ; newSet
{'q', 'x', 'a'} # same as above
>>>
>>> {'a', 'q', 'x'} == {'q', 'x', 'a'}
True
>>>
|
Assignments
[edit | edit source]
String str1 = '''
Indiana , Kentucky , Nebraska , California , Oregon , Washington , Hawaii , Alaska ,
Arizona , Utah , Nevada , Idaho , New Mexico , Colorado , Wyoming , Montana , Texas
, Oklahoma , Kansas , Nebraska , South Dakota , North D , Louisiana , Ark , Missouri ,
Iowa , Illinois , Minnesota , Michigan , Mississippi , Tennessee , Alabama ,
Ohio,West V , Virginia , Michigan , Florida , Georgia , S Carolina , , N C ,
, Maryland , Delaware , New Jersey , New York , Pennsylvania , Vermont , New Hampshire
, Maine , Connecticut , , Rhode Island , Massachussetts , Maine , Wisconsin ,
'''
Why are the letters "N C" sufficient for "North Carolina"? |
Further Reading or Review
[edit | edit source]
|
References
[edit | edit source]1. Python's documentation:
"Sets", "Set Types — set, frozenset", "Displays for lists, sets and dictionaries", "Set displays"
2. Python's methods:
3. Python's built-in functions: