CS 105: TOPIC 12 DICTIONARIES Max Fowler (Computer Science) - - PowerPoint PPT Presentation

cs 105 topic 12 dictionaries
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

CS 105: TOPIC 12 – DICTIONARIES

Max Fowler (Computer Science) https://pages.github-dev.cs.illinois.edu/cs-105/web/

JULY 26, 2020

slide-2
SLIDE 2

Week 7 Video Series Topics

Dictionaries

A reminder on Key vs Value Advanced dictionary use – histograms and get Advanced dictionary use – update

slide-3
SLIDE 3

Dictionary – Key vs Value Reminder

slide-4
SLIDE 4

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

slide-5
SLIDE 5

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

slide-6
SLIDE 6

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()

slide-7
SLIDE 7

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

slide-8
SLIDE 8

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

slide-9
SLIDE 9

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

slide-10
SLIDE 10

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)

slide-11
SLIDE 11

Advanced Dictionary – get

slide-12
SLIDE 12

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

slide-13
SLIDE 13

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!!!

slide-14
SLIDE 14

Getting around the key issue – long way

histogram = {} for item in a_list: if item in histogram: histogram[item] += 1 else: histogram[item] = 1

slide-15
SLIDE 15

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

slide-16
SLIDE 16

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

slide-17
SLIDE 17

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]
slide-18
SLIDE 18

Advanced Dictionary – update

slide-19
SLIDE 19

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()

slide-20
SLIDE 20

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

slide-21
SLIDE 21

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

slide-22
SLIDE 22

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

keys”