1 / 15

CSE 231 Lab 9

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'}}

knappd
Download Presentation

CSE 231 Lab 9

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSE 231 Lab 9

  2. Topics to cover NestedDictionaires sets

  3. 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

  4. 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'}

  5. 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])

  6. 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.

  7. 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'}

  8. 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

  9. 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)

  10. 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

  11. 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

  12. 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

  13. 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)

  14. 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)

  15. 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}

More Related