32
Nested 2 Chris Gregg Based on Slides by Chris Piech + Mehran Sahami CS106A, Stanford University

Nested 2 - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/18-Nested... · Piech, CS106A, Stanford University Nested 2 Chris Piech + Mehran Sahami CS106A, Stanford University

  • Upload
    others

  • View
    6

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Nested 2 - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/18-Nested... · Piech, CS106A, Stanford University Nested 2 Chris Piech + Mehran Sahami CS106A, Stanford University

Nested 2Chris Gregg

Based on Slides by Chris Piech + Mehran SahamiCS106A, Stanford University

Page 2: Nested 2 - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/18-Nested... · Piech, CS106A, Stanford University Nested 2 Chris Piech + Mehran Sahami CS106A, Stanford University

Housekeeping

• If you downloaded the starter code for Assignment 5 before 8am on Wednesday, July 22nd, either re-download it, or check this Ed post to see how to modify the text files to include a newline at the end of each file.

• See this Ed post to see how to read all the lines from afile all at once (instead of with a loop).

Page 3: Nested 2 - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/18-Nested... · Piech, CS106A, Stanford University Nested 2 Chris Piech + Mehran Sahami CS106A, Stanford University

Learned about Collections

Page 4: Nested 2 - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/18-Nested... · Piech, CS106A, Stanford University Nested 2 Chris Piech + Mehran Sahami CS106A, Stanford University

Listindex -> value

Page 5: Nested 2 - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/18-Nested... · Piech, CS106A, Stanford University Nested 2 Chris Piech + Mehran Sahami CS106A, Stanford University

Dictionarykey -> value

Page 6: Nested 2 - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/18-Nested... · Piech, CS106A, Stanford University Nested 2 Chris Piech + Mehran Sahami CS106A, Stanford University

Listmy_list = [‘a’, ‘b’, ‘c’]

print(my_list[1])

for i in range(len(my_list)):value = my_list[i]print(i, value)

my_dict = {‘x’:’a’, ‘y’:’b, ‘z’:’c’

}

print(my_list[‘y’])

for key in my_dict:value = my_dict[key]print(key, value)

a b c

0 1 2

my_list

a b c

‘x’ ‘y’ ‘z’

my_dict

Dictionary

indices keys

Page 7: Nested 2 - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/18-Nested... · Piech, CS106A, Stanford University Nested 2 Chris Piech + Mehran Sahami CS106A, Stanford University

Listmy_list = [

‘a’, ‘b’, ‘c’

]

print(my_list[1])

for i in range(len(my_list)):value = my_list[i]print(i, value)

my_dict = {‘x’:’a’, ‘y’:’b, ‘z’:’c’

}

print(my_list[‘y’])

for key in my_dict:value = my_dict[key]print(key, value)

a b c

0 1 2

my_list

a b c

‘x’ ‘y’ ‘z’

my_dict

Dictionary

indices keys

Page 8: Nested 2 - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/18-Nested... · Piech, CS106A, Stanford University Nested 2 Chris Piech + Mehran Sahami CS106A, Stanford University

Listmy_list = [‘a’, ‘b’, ‘c’]

print(my_list[1])

for i in range(len(my_list)):value = my_list[i]print(i, value)

my_dict = {‘x’:’a’, ‘y’:’b, ‘z’:’c’

}

print(my_list[‘y’])

for key in my_dict:value = my_dict[key]print(key, value)

a b c

0 1 2

my_list

a b c

‘x’ ‘y’ ‘z’

my_dict

Dictionary

indices keys

Page 9: Nested 2 - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/18-Nested... · Piech, CS106A, Stanford University Nested 2 Chris Piech + Mehran Sahami CS106A, Stanford University

Listmy_list = [‘a’, ‘b’, ‘c’]

print(my_list[1])

for i in range(len(my_list)):value = my_list[i]print(i, value)

my_dict = {‘x’:’a’, ‘y’:’b, ‘z’:’c’

}

print(my_list[‘y’])

for key in my_dict:value = my_dict[key]print(key, value)

a b c

0 1 2

my_list

a b c

‘x’ ‘y’ ‘z’

my_dict

Dictionary

indices keys

Page 10: Nested 2 - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/18-Nested... · Piech, CS106A, Stanford University Nested 2 Chris Piech + Mehran Sahami CS106A, Stanford University

Listmy_list = [‘a’, ‘b’, ‘c’]

print(my_list[1])

for i in range(len(my_list)):value = my_list[i]print(i, value)

my_dict = {‘x’:’a’, ‘y’:’b, ‘z’:’c’

}

print(my_list[‘y’])

for key in my_dict:value = my_dict[key]print(key, value)

a b c

0 1 2

my_list

a b c

‘x’ ‘y’ ‘z’

my_dict

Dictionary

indices keys

Page 11: Nested 2 - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/18-Nested... · Piech, CS106A, Stanford University Nested 2 Chris Piech + Mehran Sahami CS106A, Stanford University

Ultimate CS106A: Reverse a Dict

Normal Dict:

Key -> Value

Reversed Dict:

Value -> Keys

Claim: understanding this single example is most indicative of mastery in CS106A

Page 12: Nested 2 - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/18-Nested... · Piech, CS106A, Stanford University Nested 2 Chris Piech + Mehran Sahami CS106A, Stanford University

Ultimate CS106A: Reverse a Dict

ages = {'Mehran':50,'Gary':70,'Chris':32,'Wil':23,'Adele':32,'Lionel':32,'Rihanna':32,'Stephen':32

}reversed = {

50:['Mehran'],70:['Gary'],32:['Chris', 'Adele', 'Lionel', 'Rihanna', 'Stephen’],23:['Wil']

}

Page 13: Nested 2 - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/18-Nested... · Piech, CS106A, Stanford University Nested 2 Chris Piech + Mehran Sahami CS106A, Stanford University

End Review

Page 14: Nested 2 - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/18-Nested... · Piech, CS106A, Stanford University Nested 2 Chris Piech + Mehran Sahami CS106A, Stanford University

Learning Goals1. Mix and match lists and dictionaries

Page 15: Nested 2 - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/18-Nested... · Piech, CS106A, Stanford University Nested 2 Chris Piech + Mehran Sahami CS106A, Stanford University

File Types.py

import json

def main():file = open(‘ages.json’)data = json.load(file)for name in data:

age = data[name]print(name, age)

.txtOnce upon a time there was a rabbit named George. George was a wonderful animal whose goal in life was to learn to code. So George signed up for CS106A. Everything was great until one day...

.csvpastel blue,72,100,175baby blue,182,226,245purple,130,64,234blue,75,49,234light blue,76,215,249olive green,111,145,122brown,88,70,1

.json

mystery

Page 16: Nested 2 - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/18-Nested... · Piech, CS106A, Stanford University Nested 2 Chris Piech + Mehran Sahami CS106A, Stanford University

How would you store this dictionary in a file?

ages{

"Chris":48,"Gary":70,"Snoopy":52,"Wil":23,"Rihanna":32,"Adele":32

}

Page 17: Nested 2 - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/18-Nested... · Piech, CS106A, Stanford University Nested 2 Chris Piech + Mehran Sahami CS106A, Stanford University

JSON: A way to teach Nested Structures

JSON: file which stores a nested datastructure in human readable text

{"Chris":48,"Gary":70,"Snoopy":52,"Wil":23,"Rihanna":32,"Adele":32

}

ages.jsonimport json

def main():file = open(‘ages.json’)data = json.load(file)for name in data:

age = data[name]print(name, age)

print_ages.py

Page 18: Nested 2 - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/18-Nested... · Piech, CS106A, Stanford University Nested 2 Chris Piech + Mehran Sahami CS106A, Stanford University

JSON: A way to teach Nested Structures

JSON: file which stores a nested datastructure in human readable text

{"Chris":48,"Gary":70,"Snoopy":52,"Wil":23,"Rihanna":32,"Adele":32

}

ages.jsonimport json

def main():file = open(‘ages.json’)data = json.load(file)for name in data:

age = data[name]print(name, age)

print_ages.py

Page 19: Nested 2 - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/18-Nested... · Piech, CS106A, Stanford University Nested 2 Chris Piech + Mehran Sahami CS106A, Stanford University

JSON: A way to teach Nested Structures

JSON: file which stores a nested datastructure in human readable text

{"Chris":48,"Gary":70,"Snoopy":52,"Wil":23,"Rihanna":32,"Adele":32

}

ages.jsonimport json

def main():file = open(‘ages.json’)data = json.load(file)for name in data:

age = data[name]print(name, age)

print_ages.py

Page 20: Nested 2 - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/18-Nested... · Piech, CS106A, Stanford University Nested 2 Chris Piech + Mehran Sahami CS106A, Stanford University

JSON: A way to teach Nested Structures

JSON: file which stores a nested datastructure in human readable text

{"Chris":48,"Gary":70,"Snoopy":52,"Wil":23,"Rihanna":32,"Adele":32

}

ages.jsonimport json

def main():file = open(‘ages.json’)data = json.load(file)for name in data:

age = data[name]print(name, age)

print_ages.py

Page 21: Nested 2 - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/18-Nested... · Piech, CS106A, Stanford University Nested 2 Chris Piech + Mehran Sahami CS106A, Stanford University

JSON: A way to teach Nested Structures

JSON: file which stores a nested datastructure in human readable text

{"Chris":48,"Gary":70,"Snoopy":52,"Wil":23,"Rihanna":32,"Adele":32

}

ages.jsonimport json

def main():file = open(‘ages.json’)data = json.load(file)for name in data:

age = data[name]print(name, age)

print_ages.py

Page 22: Nested 2 - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/18-Nested... · Piech, CS106A, Stanford University Nested 2 Chris Piech + Mehran Sahami CS106A, Stanford University

JSON: A way to teach Nested Structures

JSON: file which stores a nested datastructure in human readable text

{"Chris":48,"Gary":70,"Snoopy":52,"Wil":23,"Rihanna":32,"Adele":32

}

ages.jsonimport json

def main():file = open(‘ages.json’)data = json.load(file)for name in data:

age = data[name]print(name, age)

print_ages.py

Page 23: Nested 2 - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/18-Nested... · Piech, CS106A, Stanford University Nested 2 Chris Piech + Mehran Sahami CS106A, Stanford University

Lets give it a whirl!

type is a function which tells you a variable’s type!

Page 24: Nested 2 - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/18-Nested... · Piech, CS106A, Stanford University Nested 2 Chris Piech + Mehran Sahami CS106A, Stanford University

JSON: A way to teach Nested Structures

{"Chris":48,"Gary":70,"Snoopy":52,"Wil":23,"Rihanna":32,"Adele":32

}

ages.jsonimport json

# load datadata = json.load(open(‘ages.json’))

# save datajson.dump(data, open(‘ages.json’))

Page 25: Nested 2 - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/18-Nested... · Piech, CS106A, Stanford University Nested 2 Chris Piech + Mehran Sahami CS106A, Stanford University

Weekly Weather

Page 26: Nested 2 - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/18-Nested... · Piech, CS106A, Stanford University Nested 2 Chris Piech + Mehran Sahami CS106A, Stanford University

Mindset Population Datahttps://www.youtube.com/watch?v=jbkSRLYSojo

Page 27: Nested 2 - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/18-Nested... · Piech, CS106A, Stanford University Nested 2 Chris Piech + Mehran Sahami CS106A, Stanford University

Mindset Raw DataPopulation GDP

Life Expectancy

Page 28: Nested 2 - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/18-Nested... · Piech, CS106A, Stanford University Nested 2 Chris Piech + Mehran Sahami CS106A, Stanford University

PythonVariable

Mindset Data Visualization

Page 29: Nested 2 - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/18-Nested... · Piech, CS106A, Stanford University Nested 2 Chris Piech + Mehran Sahami CS106A, Stanford University

PythonVariable

Mindset Data VisualizationStep 1: load the data into a python variable

Page 30: Nested 2 - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/18-Nested... · Piech, CS106A, Stanford University Nested 2 Chris Piech + Mehran Sahami CS106A, Stanford University

PythonVariable

Mindset Data VisualizationStep 2: visualize the python variable

Page 31: Nested 2 - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/18-Nested... · Piech, CS106A, Stanford University Nested 2 Chris Piech + Mehran Sahami CS106A, Stanford University

{"Afghanistan": {

"life": [28.21, 28.2, 28.19, … , 53.8],”pop”:[3280000, 3284351, … , 32526562],“gdp”:[603.0, 604.0, … , 1925.0]

},

“Albania”: {"life": […],”pop": […],“gdp”:[…]

}

… }

Mindset Data

{”1800":{

”Afghanistan”:{“life”:28.21, “pop”:3280000, “gdp”:603.0},”Albania”:{“life”:28.2, “pop”:3284351, “gdp”:604.0},…“Zimbabwe”:{“life”:20.8, “pop”: 12226542, “gdp”:98.0}

},

“1801”: {“Afghanistan”:{…}“Albania”:{…}…“Zimbabwe”:{…}

}…

}

AB

PythonVariable

Page 32: Nested 2 - web.stanford.eduweb.stanford.edu/class/cs106a/lectures/18-Nested... · Piech, CS106A, Stanford University Nested 2 Chris Piech + Mehran Sahami CS106A, Stanford University

Lets do it!