Updated
This commit is contained in:
22
AdvancedTutorials/7_Sets.py
Normal file
22
AdvancedTutorials/7_Sets.py
Normal file
@ -0,0 +1,22 @@
|
||||
#Sets are lists with no duplicate entries. Let's say you want to collect a list of words used in a paragraph:
|
||||
print(set("my name is Eric and Eric is my name".split()))
|
||||
|
||||
|
||||
# Sets are a powerful tool in Python since they have the ability to calculate differences and intersections between other sets. For example, say you have a list of participants in events A and B:
|
||||
#To find out which members attended both events, you may use the "intersection" method:
|
||||
|
||||
a = set(["Jake", "John", "Eric"])
|
||||
print(a)
|
||||
b = set(["John", "Jill"])
|
||||
print(b)
|
||||
|
||||
print(a.intersection(b))
|
||||
|
||||
#To find out which members attended only one of the events, use the "symmetric_difference" method:
|
||||
print(a.symmetric_difference(b))
|
||||
|
||||
#To find out which members attended only one event and not the other, use the "difference" method:
|
||||
print(a.difference(b))
|
||||
|
||||
#To receive a list of all participants, use the "union" method:
|
||||
print(a.union(b))
|
Reference in New Issue
Block a user