Browse Source

added new file with examples of using the set data type/object

master
T. Meissner 10 years ago
parent
commit
61cf2c9b67
1 changed files with 39 additions and 0 deletions
  1. +39
    -0
      python_3_oop/chapter06/sets.py

+ 39
- 0
python_3_oop/chapter06/sets.py View File

@ -0,0 +1,39 @@
song_library = [("Phantom of the opera", "Sarah Brightman"),
("Knocking on heaven's door", "Guns N' Roses"),
("Captain Nemo", "Sarah Brightman"),
("Patterns in the ivy", "Opeth"),
("November rain", "Guns N' Roses"),
("Beautiful", "Sarah Brightman"),
("Mal's song", "Vixy and Tony")]
artists = set()
for song, artist in song_library:
artists.add(artist)
print(artists)
for artist in artists:
print("{} plays good music".format(artist))
alpabetical = list(artists)
alpabetical.sort()
print(alpabetical)
my_artists = artists
auburns_artists = {"Nickelback", "Guns N' Roses", "Savage Garden"}
print("All: {}".format(my_artists.union(auburns_artists)))
print("Both: {}".format(my_artists.intersection(auburns_artists)))
print("Either but not both: {}".format(my_artists.symmetric_difference(auburns_artists)))
bands = {"Guns N' Roses", "Opeth"}
print("my_artists is to bands:")
print("issuperset: {}".format(my_artists.issuperset(bands)))
print("issubset: {}".format(my_artists.issubset(bands)))
print("difference: {}".format(my_artists.difference(bands)))
print("*" * 20)
print("issuperset: {}".format(bands.issuperset(my_artists)))
print("issubset: {}".format(bands.issubset(my_artists)))
print("difference: {}".format(bands.difference(my_artists)))

Loading…
Cancel
Save