CS 105: TOPIC 12 DICTIONARIES Max Fowler (Computer Science) - - PowerPoint PPT Presentation
CS 105: TOPIC 12 DICTIONARIES Max Fowler (Computer Science) - - PowerPoint PPT Presentation
CS 105: TOPIC 12 DICTIONARIES Max Fowler (Computer Science) https://pages.github-dev.cs.illinois.edu/cs-105/web/ JULY 26, 2020 Week 7 Video Series Topics Dictionaries A reminder on Key vs Value Advanced dictionary use
Week 7 Video Series Topics
Dictionaries
A reminder on Key vs Value Advanced dictionary use – histograms and get Advanced dictionary use – update
Dictionary – Key vs Value Reminder
A reminder of dictionary structure
You’ve been using dictionaries for a while now. animals_on_island = {“puffin”:20, “alpaca”:25,
“cats”:10}
Key – animals Value – The population of that animal
Print the total number of animals on the island
Effectively, sum the values in the dictionary. Can we just do….:
sv = 0 for val in animals_on_island: sv+= val
Variable names aren’t magic
‘val’ has no meaning to Python – it is just an identifier for a
variable
To get access to the values, we need to do one of three
things:
1) Use the keys 2) Use .values() 3) Use .items()
Approach one – using keys to access values
def animal_pop(animals_on_island): total_pop = 0 for animal in animals_on_island: total_pop += animals_on_island[animal] return total_pop
Approach two – using .values()
.values() is a function that lets us get the values in a sequence
def animal_pop2(animals_on_island): total_pop = 0 for pop in animals_on_island.values(): total_pop += pop return total_pop
Approach three – using .items()
.items() gives us both the key AND the value for each pair – so we
need two variables def animal_pop3(animals_on_island): total_pop = 0 for animal, pop in animals_on_island.items(): total_pop += pop return total_pop
Video question
Assuming my_list is a list of strings and my_dict is a dictionary with
int keys and string values, what does this code do? s = 0 for key, val in my_dict.items(): if val in my_list: s += key print(s)
Advanced Dictionary – get
Dictionaries – common paradigm
Given a list/file of something, make a histogram of
the items in the list
What’s a histogram? A distribution A dictionary of values and their counts
Problem – updating counts.
Given histogram = {“apple”:5, “pineapple”:2”} histogram[“apple”] += 1 #Updates to six histogram[“orange”] +=1 #A key error – orange doesn’t exist yet!!!
Getting around the key issue – long way
histogram = {} for item in a_list: if item in histogram: histogram[item] += 1 else: histogram[item] = 1
Dictionaries - get
get is a method of dictionaries that simplifies this process How does get work? a_dictionary.get(key, default) If key exists, get the value If the key is not in the dictionary, use the default value
provided
Simplified down using get
histogram = {} for item in a_list: histogram[item] = histogram.get(item, 0) + 1
Notice… We use 0 as the default, so we can always “+1” No more if statement!! No += -> have to use regular assignment
Video question – what does this code do?
def fn(other_dict, data_dict): for key in data_dict:
- ther_dict[key] = other_dict.get(key,0) + data_dict[key]
Advanced Dictionary – update
Dictionary updates
In the last video question, we updated a dictionary using
the key-value pairs from another
Dictionaries have a method for that, but there is a
catch… update()
update
a_dict.update(another_dict) For each key in another_dict…
If the key does not exist in a_dict, add it If the key DOES exist in a_dict, CHANGE THE VALUE
Use cases for updating dictionaries
Use a loop and get IF
You want to add/subtract/somehow combine values for
existing keys
Use update if
You want to update the dictionary, erasing old data, OR the
two dictionaries do not share any keys
Video question
Is this best solved with the use of .update() or with a loop
and .get()?
“Write a function which takes two dictionaries – old and
- new. It should use the new dictionary to update the keys in
- ld, replacing any existing values and adding any new