Sets
Again remember that a set cannot be indexed and is denoted by a pair of {}. A set is also unordered so print calls will give a random listing out to your screen. You can only add items to a set, unless you want to subtract items AND you're using the set's remove() or discard() methods. Both of these subtracting methods take one argument.
add() will insert one item; use the update() method along with square braces as part of the argument to add two or more items. Interestingly, if you leave off the square braces in the update method call to a set, the individual characters of a text string are parsed and inserted randomly but not duplicated.
sports = {"baseball", "football", "basketball"}
sports.update(["hockey", "volleyball"])
print(sports)
print(type(sports))
sports.update("skiing", "hiking" )
print(sports)
print(type(sports))
# results in
{'hockey', 'baseball', 'football', 'basketball', 'volleyball'} # random insertion
<class 'set'>
{'i', 'hockey', 'g', 'n', 'h', 'baseball', 's', 'k', 'football', 'basketball', 'volleyball'}
<class 'set'>
# notice how the ing ending of each sport (skiing or hiking) is only shown by one of each of 'i' 'n' 'g'
# same for the k: ski vs hik
# This comes back to the idea that duplication is not allowed in sets, even single character strings
There's a subtle difference between the function of the remove and discard methods, even though they look at the surface to be doing the same thing by a different name. If you want to try to remove("something") and "something" is not found in a set, the remove method gives you an error called a keyerror. Not the discard method: if not found you get no error; it'll just returned back the set with no changes.
You cannot use a pair of square braces inside of a remove or discard method call to try and index because again the set is not indexable; you'll get an error that says 'unhashable type list'. The pop() method will return one value which is the subtracted item, however, it's what will be the right most value and it's a random selection so you don't know what you're going to get.
You can join two sets doing using the union() method, however this requires a third variable to accept the result of the joining sets 1 and 2. union will also exclude duplicates.
days = {"Sunday", "Tuesday", "Friday"}
other_days = {"Monday", "Wednesday", "Thursday", "Saturday"}
week = days.union(other_days)
# results in week containing all 7 days but in a random order. The random order however
# remains fixed until another method call is made.
days = {"Sunday", "Tuesday", "Friday"}
other_days = {"Monday", "Wednesday", "Thursday", "Saturday"}
# need new 3rd variable week
week = days.union(other_days)
print(week)
print(week)
other_days = {"Monday", "Wednesday", "Thursday", "Saturday"}
# need new 3rd variable week
week = days.union(other_days)
print(week)
print(week)
# need new 3rd variable all_days
all_days = other_days.union(days)
print(all_days)
print(all_days)
# no 3rd variable with update()
days.update(other_days)
print(days)
print(days)
all_days = other_days.union(days)
print(all_days)
print(all_days)
# no 3rd variable with update()
days.update(other_days)
print(days)
print(days)
####### RESULTS ###############
{'Monday', 'Tuesday', 'Sunday', 'Saturday', 'Wednesday', 'Friday', 'Thursday'}
{'Monday', 'Tuesday', 'Sunday', 'Saturday', 'Wednesday', 'Friday', 'Thursday'}
{'Tuesday', 'Friday', 'Sunday', 'Saturday', 'Wednesday', 'Monday', 'Thursday'}
{'Tuesday', 'Friday', 'Sunday', 'Saturday', 'Wednesday', 'Monday', 'Thursday'}
{'Monday', 'Tuesday', 'Sunday', 'Saturday', 'Wednesday', 'Friday', 'Thursday'}
{'Monday', 'Tuesday', 'Sunday', 'Saturday', 'Wednesday', 'Friday', 'Thursday'}
{'Monday', 'Tuesday', 'Sunday', 'Saturday', 'Wednesday', 'Friday', 'Thursday'}
{'Tuesday', 'Friday', 'Sunday', 'Saturday', 'Wednesday', 'Monday', 'Thursday'}
{'Tuesday', 'Friday', 'Sunday', 'Saturday', 'Wednesday', 'Monday', 'Thursday'}
{'Monday', 'Tuesday', 'Sunday', 'Saturday', 'Wednesday', 'Friday', 'Thursday'}
{'Monday', 'Tuesday', 'Sunday', 'Saturday', 'Wednesday', 'Friday', 'Thursday'}
####### RESULTS ###############
The set data type has a rich number of methods; consult the following link to get more details:
No comments:
Post a Comment