Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 231 Lab 9.

Similar presentations


Presentation on theme: "CSE 231 Lab 9."— Presentation transcript:

1 CSE 231 Lab 9

2 Nested Dictionaires sets
Topics to cover Nested Dictionaires 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': ‘Imen', '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'] {'name': 'Imen', 'age': '40', 'sex': 'Female', 'married': 'Yes'} {'name': 'Imen', 'age': '40', 'sex': 'Female'}

5 What is Nested Dictionary?
Person ID: 1 name: Zach age: 7 sex: Male Person ID: 2 name: Imen 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). Unordered Collection 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 {'b', 'd', 'a', 'c'} {'r', 'd'} 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: Print(S) {'b', 'd', 'a', 'c'} S = {x for x in 'abracadabra' if x not in 'abc'} Print(S) {'r', 'd'}

8 True #A and B are equal sets
Create and update a Set Creating a set A = {1, 2, 3} B = { 3, 2, 3, 1} True #A and 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! Print(S) {'b', 100, 'd', 'a', 'c'} Print(S) {'b', 'd', 'a', 'c'} Print(S) KeyError: 100

10 Patterns Ints: Strings: Lists: 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: S = set() # initialize with empty set
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_a or 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}


Download ppt "CSE 231 Lab 9."

Similar presentations


Ads by Google