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 ID: 904538
Download The PPT/PDF document "CSE 231 Lab 9 Topics to" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
CSE 231
Lab 9
Slide2Topics to
cover
Nested
Dictionaires
sets
Slide3What
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
Slide4What
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'}
people
=
{1: {'name': ‘Zach', 'age': '7', 'sex': 'Male'},
2
: {'name': ‘Ann', 'age': ‘40', 'sex': 'Female'}}
Slide5What
is Nested Dictionary?
Iterating through a nested dictionary:
for id, info in people.items(): print("\nPerson ID:", id)
for key in info
: print(key
+ ':', info[key])
Person ID: 1
name: Zach
age: 7
sex: Male
Person ID: 2
name:
Ann
age: 40
sex:
Female
people
=
{1: {'name': ‘Zach', 'age': '7', 'sex': 'Male'},
2
: {'name': ‘Ann', 'age': ‘40', 'sex': 'Female'}}
Slide6Sets
Think of mathematics
.
All items in a set are unique (no duplicates).
Unordered Collection of unique objectsLast data structureUse { } just like dictionariesBut not key:value pairs
Example: { 1, 2, 3 }The set doesn’t maintain elements in any particular order.
Slide7Create 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 duplicatesset comprehensions are also supported:
{'b', 'd', 'a', 'c'}
p
rint(S)
S = {x for x in 'abracadabra' if x not in 'abc'}
print(S)
{'r', 'd'}
Slide8Create and update a Set
Creating a
set
A
= {1, 2, 3} B = { 3, 2, 3, 1}print(A == B)
True #A
and B are equal sets
The order of elements is unimportant and duplicates are removed
Slide9Create and update a Set
S = {'b', 'd', 'a', 'c'}
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'}
p
rint(S
)
{'b',
'd
', 'a', 'c'}
p
rint(S
)
KeyError
: 100
p
rint(S
)
Slide10Patterns
Ints
:
x = 0 #
initialize at zerox += 1 # increment the counterStrings:s = ‘’ # initialize with empty strings += ch # concatenate characters Lists:L = [] # initialize with empty listL.append(value) # append values to the list
Slide11Patterns
Dictionaries:
D = { } #
initialize with empty
dictionaryS = “aabacdbacd” # we have a string, we want to count all the charactersfor 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
Slide12Patterns
Sets:
We have
a dictionary
file and we want to create a set of all its wordsS = set() # initialize with empty setfp = open(“dictionary.txt”)for line in fp: set.add(line.strip()) # add the word to the set, it ignores the duplicates
Slide13Useful Function, Methods & Operators
l
en
(S) returns the size of the set
check whether an element belongs to a set using the keyword in Union:set_a | set_bset_a.union(set_b)Intersection:set_a & set_bset_a.intersection(set_b)
Slide14Useful Function, Methods & Operators
Symmetric_Difference
: the set of elements in precisely one of
set_a
or set_bSet_a ^ set_bDifference: the set of elements in set_a but not set_bset_a - set_bset_a.difference(set_b)
Slide15Sets 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}
{1, 2, 3, 6, 7, 8}
{8, 6, 7}