150 likes | 162 Views
CSE 231 Lab 9. Topics to cover. Nested Dictionaires sets. What is Nested Dictionary?. A dictionary inside a dictionary Creating a nested dictionary: people = {1: {'name': ‘Zach', 'age': '7', 'sex': 'Male'}, 2: {'name': ‘ Ann ', 'age': ‘40', 'sex': 'Female'}}
E N D
Topics to cover NestedDictionaires sets
What is Nested Dictionary? • A dictionary inside a dictionary • Creating a nested dictionary: • people= {1: {'name': ‘Zach', 'age': '7', 'sex': 'Male'}, 2: {'name': ‘Ann', 'age': ‘40', 'sex': 'Female'}} • print(people[1]['name']) • print(people[1]['age']) • print(people[1]['sex']) Zach 7 Male
What is Nested Dictionary? • Modifying a nested dictionary: • people[2]['married'] = 'Yes‘ print(people[2]) • del people[2]['married'] print(people[2]) {'name': ‘Ann', 'age': '40', 'sex': 'Female', 'married': 'Yes'} {'name': ‘Ann', 'age': '40', 'sex': 'Female'}
What is Nested Dictionary? Person ID: 1 name: Zach age: 7 sex: Male Person ID: 2 name: Ann age: 40 sex: Female • Iterating through a nested dictionary: for id, info in people.items(): print("\nPerson ID:", id) for key in info: print(key + ':', info[key])
Sets • Think of mathematics. • All items in a set are unique (no duplicates). • UnorderedCollection of unique objects • Last data structure • Use { } just like dictionaries • But not key:value pairs • Example: { 1, 2, 3 } • The set doesn’t maintain elements in any particular order.
Create and update a Set • Creating a set • S = set() # S = { } creates a dictionary! • S = { 20, 5, 10 } • S = set(“abcabbcd”) # Works with any iterable, ignores duplicates • set comprehensions are also supported: {'b', 'd', 'a', 'c'} Print(S) S = {x for x in 'abracadabra' if x not in 'abc'} Print(S) {'r', 'd'}
Create and update a Set • Creating a set • A= {1, 2, 3} • B = { 3, 2, 3, 1} True #Aand B are equal sets Print(A == B) The order of elements is unimportant and duplicates are removed
Create and update a Set • Adding/discarding an object • S.add(100) # adds the object, ignores duplicates • S.discard(100) # discards the object if it exists • S.remove(100) # removes the object from set, but raises KeyError if the object doesn’t exist! {'b', 100, 'd', 'a', 'c'} Print(S) {'b', 'd', 'a', 'c'} Print(S) KeyError: 100 Print(S)
Patterns • Ints: • x = 0 # initialize at zero • x += 1 # increment the counter • Strings: • s = ‘’ # initialize with empty string • s += ch # concatenate characters • Lists: • L = [] # initialize with empty list • L.append(value) # append values to the list
Patterns • Dictionaries: D = { } # initialize with empty dictionary S = “aabacdbacd” # we have a string, we want to count all the characters for ch in S: if ch in D: # check to see if the key exists in the dictionary D[ch] += 1 # increment the value if it exists else: D[ch] = 1 # set the value to 1 if it doesn’t exist
Patterns • Sets: • We have a dictionary file and we want to create a set of all its words S = set() # initialize with empty set fp = open(“dictionary.txt”) for line in fp: set.add(line.strip()) # add the word to the set, it ignores the duplicates
Useful Function, Methods & Operators • len(S) returns the size of the set • check whether an element belongs to a set using the keyword in • Union: • set_a| set_b • set_a.union(set_b) • Intersection: • set_a& set_b • set_a.intersection(set_b)
Useful Function, Methods & Operators • Symmetric_Difference: the set of elements in precisely one of set_aor set_b • Set_a ^ set_b • Difference: the set of elements in set_a but not set_b • set_a - set_b • set_a.difference(set_b)
Sets examples A = {1, 2, 3, 4, 5} B = {4, 5, 6, 7, 8} print(A | B) print(A & B) print(A - B) print(B - A) print(A ^ B) {1, 2, 3, 4, 5, 6, 7, 8} {4, 5} {1, 2, 3} {8, 6, 7} {1, 2, 3, 6, 7, 8}