>>> simpleset = {'a', 'b', 'c', 'd'}
>>> type(simpleset)
<class 'set'>
>>> print (simpleset)
{'c', 'd', 'a', 'b'}
This shows a number of things.
When creating a new set with elements in it, you use curly brackets {} (a.k.a. braces) like with dictionaries.
Elements are separated with commas.
>>> newset = simpleset | {'d', 'e', 'f', 'g'}
>>> print (newset)
{'g', 'a', 'b', 'f', 'c', 'd', 'e'}
>>>
Unlike strings, lists and tuples, you cannot use + to concatenate sets. Instead you use | for a union of two sets. This will return all the elements found either in one set or the other. As you can see in newset, the elements that are common to both sets are only found once in newset.
As sets are unordered, the elements will be printed out in any order, apparently randomly shuffled.
As well as union, there are other operations that can be performed on sets. For example:
>>> nextset = {'f', 'g', 'h', 'i', 'j'}& gives the intersection of two sets, i.e. the elements that occur in both of them.
>>> print (newset)
{'g', 'a', 'b', 'f', 'c', 'd', 'e'}
>>> print (newset & nextset)
{'f', 'g'}
>>>
To find the elements that are in one set or the other but not in both (i.e. the opposite of intersection), you find the symmetrical difference, using ^
>>> print (newset)or if you want the elements that are in just one set but not another, you can use - for difference.
{'g', 'a', 'b', 'f', 'c', 'd', 'e'}
>>> print (nextset)
{'j', 'g', 'i', 'f', 'h'}
>>> print (newset ^ nextset)
{'c', 'j', 'a', 'i', 'b', 'd', 'h', 'e'}
>>>
>>> print (newset - nextset)Although I am using strings in this simple example, like other collections (lists, tuples and dictionaries), sets can have other data types as their elements.
{'c', 'a', 'b', 'd', 'e'}
>>>
If they are unordered , then why use sets rather than lists?
The simple answer is that they are useful when you just want one copy of each value in the collection - i.e. removing duplicates.
And you can use the built-in function set() to convert other collections into lists.
For example, you have a list of email addresses compiled from senders in your inbox. You want them in alphabetical order but also want to remove duplicates
>>> emaillist = ['alice@python.com', 'eric@python.com', 'charlie@python.com', 'bob@python.com', 'alice@python.com', 'dave@python.com', 'bob@python.com', 'bob@python.com', 'dave@python.com']Thus we can convert to a set using the set() function to remove duplicates, then convert back to list using the list() function to then sort into order. You can also convert between tuples and sets.
>>> print (type(emaillist))
<class 'list'>
>>> emailset = set(emaillist)
>>> print (type(emailset))
<class 'set'>
>>> print (emailset)
{'eric@python.com', 'dave@python.com', 'bob@python.com', 'charlie@python.com', 'alice@python.com'}
>>> emaillist = list(emailset)
>>> emaillist.sort()
>>> print (emaillist)
['alice@python.com', 'bob@python.com', 'charlie@python.com', 'dave@python.com', 'eric@python.com']
>>>
No comments:
Post a Comment