Lecture 16 - cs.cornell.edu · Lecture 16. Announcements for This Lecture 10/18/18 Nested Lists and...

23
Nested Lists and Dictionaries Lecture 16

Transcript of Lecture 16 - cs.cornell.edu · Lecture 16. Announcements for This Lecture 10/18/18 Nested Lists and...

Page 1: Lecture 16 - cs.cornell.edu · Lecture 16. Announcements for This Lecture 10/18/18 Nested Lists and Dictionaries 2 •Regrades are now open §Only for MAJOR mistakes §You might losepoints

Nested Lists and Dictionaries

Lecture 16

Page 2: Lecture 16 - cs.cornell.edu · Lecture 16. Announcements for This Lecture 10/18/18 Nested Lists and Dictionaries 2 •Regrades are now open §Only for MAJOR mistakes §You might losepoints

Announcements for This Lecture

10/18/18 2Nested Lists and Dictionaries

• Regrades are now open§ Only for MAJOR mistakes

§ You might lose points

• The regrade process§ Ask in Gradescope

§ Tell us what to look for

§ If valid, we will respond

§ We will also update CMS

Assignments/ReadingPrelim and Regrades

• Should be working on A4§ Tasks 1-2 by tomorrow

§ Task 3 by the weekend

§ Recursion next week

• Reading: Chapters 15, 16§ Chapter 17 for next week

§ Lot of reading but important

Page 3: Lecture 16 - cs.cornell.edu · Lecture 16. Announcements for This Lecture 10/18/18 Nested Lists and Dictionaries 2 •Regrades are now open §Only for MAJOR mistakes §You might losepoints

Lists of Objects

• List positions are variables§ Can store base types§ But cannot store folders§ Can store folder identifiers

• Folders linking to folders§ Top folder for the list§ Other folders for contents

• Example:>>> r = introcs.RGB(255,0,0)>>> b = introcs.RGB(0,0,255)>>> g = introcs.RGB(0,255,0)>>> x = [r,b,g]

10/18/18 Nested Lists and Dictionaries 3

id10

red 255

green 0

blue 0

RGB id11

red 0

green 0

blue 255

RGB

id12

red 0

green 255

blue 0

RGB

id13x

id13

x[0]x[1]x[2]

id10id11id12

list

id12g

id11b

id10r

Page 4: Lecture 16 - cs.cornell.edu · Lecture 16. Announcements for This Lecture 10/18/18 Nested Lists and Dictionaries 2 •Regrades are now open §Only for MAJOR mistakes §You might losepoints

Lists of Objects

• List positions are variables§ Can store base types§ But cannot store folders§ Can store folder identifiers

• Folders linking to folders§ Top folder for the list§ Other folders for contents

• Example:>>> r = introcs.RGB(255,0,0)>>> b = introcs.RGB(0,0,255)>>> g = introcs.RGB(0,255,0)>>> x = [r,b,g]

10/18/18 Nested Lists and Dictionaries 4

id10

red 255

green 0

blue 0

RGB id11

red 0

green 0

blue 255

RGB

id12

red 0

green 255

blue 0

RGB

id13x

id13

x[0]x[1]x[2]

id10id11id12

list

id12g

id11b

id10r

Page 5: Lecture 16 - cs.cornell.edu · Lecture 16. Announcements for This Lecture 10/18/18 Nested Lists and Dictionaries 2 •Regrades are now open §Only for MAJOR mistakes §You might losepoints

Nested Lists

• Lists can hold any objects• Lists are objects• Therefore lists can hold other lists!

x = [1, [2, 1], [1, 4, [3, 1]], 5]x[0] x[1][1] x[2][2][1]x[2][0]

x[1] x[2] x[2][2]a = [2, 1]b = [3, 1]c = [1, 4, b]x = [1, a, c, 5]

10/18/18 Nested Lists and Dictionaries 5

Page 6: Lecture 16 - cs.cornell.edu · Lecture 16. Announcements for This Lecture 10/18/18 Nested Lists and Dictionaries 2 •Regrades are now open §Only for MAJOR mistakes §You might losepoints

Two Dimensional Lists

Table of Data Images

10/18/18 Nested Lists and Dictionaries 6

5 4 7 3

4 8 9 7

5 1 2 3

4 1 2 9

6 7 8 0

0 1 2 3

0

1

4

2

3

Store them as lists of lists (row-major order)d = [[5,4,7,3],[4,8,9,7],[5,1,2,3],[4,1,2,9],[6,7,8,0]]

0 1 2 3 4 5 6 7 8 9 101112

0123456789

101112

Each row, col has a value Each row, col has

an RGB value

Page 7: Lecture 16 - cs.cornell.edu · Lecture 16. Announcements for This Lecture 10/18/18 Nested Lists and Dictionaries 2 •Regrades are now open §Only for MAJOR mistakes §You might losepoints

Overview of Two-Dimensional Lists

• Access value at row 3, col 2:

d[3][2]• Assign value at row 3, col 2:

d[3][2] = 8• An odd symmetry

§ Number of rows of d: len(d)

§ Number of cols in row r of d: len(d[r])

10/18/18 Nested Lists and Dictionaries 7

5 4 7 3

4 8 9 7

5 1 2 3

4 1 2 9

6 7 8 0

d0 1 2 3

01

4

2

3

Page 8: Lecture 16 - cs.cornell.edu · Lecture 16. Announcements for This Lecture 10/18/18 Nested Lists and Dictionaries 2 •Regrades are now open §Only for MAJOR mistakes §You might losepoints

How Multidimensional Lists are Stored

• b = [[9, 6, 4], [5, 7, 7]]

• b holds name of a one-dimensional list§ Has len(b) elements§ Its elements are (the names of) 1D lists

• b[i] holds the name of a one-dimensional list (of ints) § Has len(b[i]) elements

10/18/18 Nested Lists and Dictionaries 8

id2

964

id3

577

id1

id2id3

id1b

9 6 45 7 7

Page 9: Lecture 16 - cs.cornell.edu · Lecture 16. Announcements for This Lecture 10/18/18 Nested Lists and Dictionaries 2 •Regrades are now open §Only for MAJOR mistakes §You might losepoints

Image Data: 2D Lists of Pixels

10/18/18 Nested Lists and Dictionaries 9

0 1 2 3 4 5 6 7 8 9 101112

0123456789

101112

id1b id1

id2id3

list

id2

id23id24

list

id23

red 255

green 255

blue 255

RGB

b[0][0] is a white pixel

Page 10: Lecture 16 - cs.cornell.edu · Lecture 16. Announcements for This Lecture 10/18/18 Nested Lists and Dictionaries 2 •Regrades are now open §Only for MAJOR mistakes §You might losepoints

Ragged Lists: Rows w/ Different Length

10/18/18 Nested Lists and Dictionaries 10

• b = [[17,13,19],[28,95]]

• Will see applications of this later

id2

171319

id3

2895

id1id1b

id2id3

012

1 10

0

Page 11: Lecture 16 - cs.cornell.edu · Lecture 16. Announcements for This Lecture 10/18/18 Nested Lists and Dictionaries 2 •Regrades are now open §Only for MAJOR mistakes §You might losepoints

Slices and Multidimensional Lists

• Only “top-level” list is copied.• Contents of the list are not altered• b = [[9, 6], [4, 5], [7, 7]]

10/18/18 Nested Lists and Dictionaries 11

id2

96

id1

id2id3

id1b

id4

id3

45id4

77

x = b[:2]

id5x

id5

id2id3

Page 12: Lecture 16 - cs.cornell.edu · Lecture 16. Announcements for This Lecture 10/18/18 Nested Lists and Dictionaries 2 •Regrades are now open §Only for MAJOR mistakes §You might losepoints

Slices and Multidimensional Lists

• Only “top-level” list is copied.• Contents of the list are not altered• b = [[9, 6], [4, 5], [7, 7]]

10/18/18 Nested Lists and Dictionaries 12

id2

96

id1

id2id3

id1b

id4

id3

45id4

77

x = b[:2]

id5x

id5

id2id3

Page 13: Lecture 16 - cs.cornell.edu · Lecture 16. Announcements for This Lecture 10/18/18 Nested Lists and Dictionaries 2 •Regrades are now open §Only for MAJOR mistakes §You might losepoints

Slices and Multidimensional Lists

• Create a nested list>>> b = [[9,6],[4,5],[7,7]]

• Get a slice>>> x = b[:2]

• Append to a row of x>>> x[1].append(10)

• x now has nested list[[9, 6], [4, 5, 10]]

• What are the contents of the list (with name) in b?

10/18/18 Nested Lists and Dictionaries 13

A: [[9,6],[4,5],[7,7]]B: [[9,6],[4,5,10]]C: [[9,6],[4,5,10],[7,7]]D: [[9,6],[4,10],[7,7]]E: I don’t know

Page 14: Lecture 16 - cs.cornell.edu · Lecture 16. Announcements for This Lecture 10/18/18 Nested Lists and Dictionaries 2 •Regrades are now open §Only for MAJOR mistakes §You might losepoints

Slices and Multidimensional Lists

• Create a nested list>>> b = [[9,6],[4,5],[7,7]]

• Get a slice>>> x = b[:2]

• Append to a row of x>>> x[1].append(10)

• x now has nested list[[9, 6], [4, 5, 10]]

• What are the contents of the list (with name) in b?

10/18/18 Nested Lists and Dictionaries 14

A: [[9,6],[4,5],[7,7]]B: [[9,6],[4,5,10]]C: [[9,6],[4,5,10],[7,7]]D: [[9,6],[4,10],[7,7]]E: I don’t know

Page 15: Lecture 16 - cs.cornell.edu · Lecture 16. Announcements for This Lecture 10/18/18 Nested Lists and Dictionaries 2 •Regrades are now open §Only for MAJOR mistakes §You might losepoints

Functions and 2D Lists

def transpose(table):"""Returns: copy of table with rows and columns swappedPrecondition: table is a (non-ragged) 2d List"""numrows = len(table) # Need number of rowsnumcols = len(table[0]) # All rows have same no. colsresult = [] # Result (new table) accumulator for m in range(numcols):

# Get the column elements at position m# Make a new list for this column# Add this row to accumulator table

return result10/18/18 Nested Lists and Dictionaries 15

1 2

3 4

5 6

1 3 5

2 4 6

Page 16: Lecture 16 - cs.cornell.edu · Lecture 16. Announcements for This Lecture 10/18/18 Nested Lists and Dictionaries 2 •Regrades are now open §Only for MAJOR mistakes §You might losepoints

Functions and 2D Lists

def transpose(table):"""Returns: copy of table with rows and columns swappedPrecondition: table is a (non-ragged) 2d List"""numrows = len(table) # Need number of rowsnumcols = len(table[0]) # All rows have same no. colsresult = [] # Result (new table) accumulator for m in range(numcols):

row = [] # Single row accumulatorfor n in range(numrows):

row.append(table[n][m]) # Create a new row listresult.append(row) # Add result to table

return result10/18/18 Nested Lists and Dictionaries 16

1 2

3 4

5 6

1 3 5

2 4 6

Page 17: Lecture 16 - cs.cornell.edu · Lecture 16. Announcements for This Lecture 10/18/18 Nested Lists and Dictionaries 2 •Regrades are now open §Only for MAJOR mistakes §You might losepoints

Functions and 2D Lists

def transpose(table):"""Returns: copy of table with rows and columns swappedPrecondition: table is a (non-ragged) 2d List"""numrows = len(table) # Need number of rowsnumcols = len(table[0]) # All rows have same no. colsresult = [] # Result (new table) accumulator for m in range(numcols):

row = [] # Single row accumulatorfor n in range(numrows):

row.append(table[n][m]) # Create a new row listresult.append(row) # Add result to table

return result10/18/18 Nested Lists and Dictionaries 17

1 2

3 4

5 6

1 3 5

2 4 6

Nest lists neednested loops

Page 18: Lecture 16 - cs.cornell.edu · Lecture 16. Announcements for This Lecture 10/18/18 Nested Lists and Dictionaries 2 •Regrades are now open §Only for MAJOR mistakes §You might losepoints

Dictionaries (Type dict)

Description

• List of key-value pairs§ Keys are unique§ Values need not be

• Example: net-ids§ net-ids are unique (a key)§ names need not be (values)§ js1 is John Smith (class ’13)§ js2 is John Smith (class ’16)

• Many other applications

Python Syntax

• Create with format:{k1:v1, k2:v2, …}

• Keys must be non-mutable§ ints, floats, bools, strings§ Not lists or custom objects

• Values can be anything• Example:

d = {'js1':'John Smith','js2':'John Smith','wmw2':'Walker White'}

10/18/18 Nested Lists and Dictionaries 18

Page 19: Lecture 16 - cs.cornell.edu · Lecture 16. Announcements for This Lecture 10/18/18 Nested Lists and Dictionaries 2 •Regrades are now open §Only for MAJOR mistakes §You might losepoints

Using Dictionaries (Type dict)• Access elts. like a list

§ d['js1'] evaluates to 'John'§ But cannot slice ranges!

• Dictionaries are mutable§ Can reassign values§ d['js1'] = 'Jane'§ Can add new keys§ d['aa1'] = 'Allen'§ Can delete keys§ del d['wmw2']

d = {'js1':'John','js2':'John','wmw2':'Walker'}

10/18/18 Nested Lists and Dictionaries 19

'wmw2'

id8

'John''John'

'Walker'

dict

'js2''js1'

Key-Value order in folder is not important

id8d

Page 20: Lecture 16 - cs.cornell.edu · Lecture 16. Announcements for This Lecture 10/18/18 Nested Lists and Dictionaries 2 •Regrades are now open §Only for MAJOR mistakes §You might losepoints

Using Dictionaries (Type dict)• Access elts. like a list

§ d['js1'] evaluates to 'John'§ But cannot slice ranges!

• Dictionaries are mutable§ Can reassign values§ d['js1'] = 'Jane'§ Can add new keys§ d['aa1'] = 'Allen'§ Can delete keys§ del d['wmw2']

d = {'js1':'John','js2':'John','wmw2':'Walker'}

10/18/18 Nested Lists and Dictionaries 20

'wmw2'

id8

'John' 'Jane''John'

'Walker'

dict

'js2''js1'

Key-Value order in folder is not important

id8d

Page 21: Lecture 16 - cs.cornell.edu · Lecture 16. Announcements for This Lecture 10/18/18 Nested Lists and Dictionaries 2 •Regrades are now open §Only for MAJOR mistakes §You might losepoints

Using Dictionaries (Type dict)• Access elts. like a list

§ d['js1'] evaluates to 'John'§ But cannot slice ranges!

• Dictionaries are mutable§ Can reassign values§ d['js1'] = 'Jane'§ Can add new keys§ d['aa1'] = 'Allen'§ Can delete keys§ del d['wmw2']

d = {'js1':'John','js2':'John','wmw2':'Walker'}

10/18/18 Nested Lists and Dictionaries 21

'wmw2'

id8

'Jane''John'

'Walker'

dict

'js2''js1'

'aa1' 'Allen'

id8d

Page 22: Lecture 16 - cs.cornell.edu · Lecture 16. Announcements for This Lecture 10/18/18 Nested Lists and Dictionaries 2 •Regrades are now open §Only for MAJOR mistakes §You might losepoints

Using Dictionaries (Type dict)• Access elts. like a list

§ d['js1'] evaluates to 'John'§ But cannot slice ranges!

• Dictionaries are mutable§ Can reassign values§ d['js1'] = 'Jane'§ Can add new keys§ d['aa1'] = 'Allen'§ Can delete keys§ del d['wmw2']

d = {'js1':'John','js2':'John','wmw2':'Walker'}

10/18/18 Nested Lists and Dictionaries 22

'wmw2'

id8

'Jane''John'

'Walker'

dict

'js2''js1'

'aa1' 'Allen'✗ ✗

Deleting key deletes both

id8d

Page 23: Lecture 16 - cs.cornell.edu · Lecture 16. Announcements for This Lecture 10/18/18 Nested Lists and Dictionaries 2 •Regrades are now open §Only for MAJOR mistakes §You might losepoints

Dictionaries and For-Loops

• Dictionaries != sequences§ Cannot slice them

• Different inside for loop § Loop variable gets the key§ Then use key to get value

• Can extract iterators with dictionary methods§ Key iterator: d.keys()§ Value iterator: d.values()§ key-value pairs: d.items()

for k in d:# Loops over keysprint(k) # keyprint(d[k]) # value

# To loop over values onlyfor v in d.values():

print(v) # value

10/18/18 Nested Lists and Dictionaries 23

See grades.py