Lecture 14 Nested Lists and Dictionaries
Announcements for This Lecture Readings Assignments • Today: Chapter 11 • A3 is due today § Survey is posted in CMS • Next Week: Sec. 5.8-5.10 § Late penalty 10%/day • Prelim, Oct 12 th 7:30-9:00 • Opportunities for help § Material up to TUESDAY § Consultants 4:30-9:30 § Study guide is posted § Will be on Piazza until Mid. • Review session Wednesday • No lab next week § Still checking place/time § Tuesday part of Fall Break § Announcement on Piazza § No special lab for Wed 10/5/17 Nested Lists and Dictionaries 2
Lists of Objects • List positions are variables id13 r id10 § Can store base types list b id11 § But cannot store folders x[0] id10 § Can store folder identifiers g id12 x[1] id11 x[2] • Folders linking to folders x id12 id13 § Top folder for the list id12 § Other folders for contents id10 id11 RGB RGB • Example: RGB red 0 red 255 >>> r = cornell.RED red 0 green 0 green 0 >>> b = cornell.BLUE green 255 blue 255 >>> g = cornell.GREEN blue 0 blue 0 >>> x = [r,b,g] 10/5/17 Nested Lists and Dictionaries 3
Lists of Objects • List positions are variables id13 r id10 § Can store base types list b id11 § But cannot store folders x[0] id10 § Can store folder identifiers g id12 x[1] id11 x[2] • Folders linking to folders x id12 id13 § Top folder for the list id12 § Other folders for contents id10 id11 RGB RGB • Example: RGB red 0 red 255 >>> r = cornell.RED red 0 green 0 green 0 >>> b = cornell.BLUE green 255 blue 255 >>> g = cornell.GREEN blue 0 blue 0 >>> x = [r,b,g] 10/5/17 Nested Lists and Dictionaries 4
Nested Lists • Lists can hold any objects • Lists are objects • Therefore lists can hold other lists! x[2] a = [2, 1] x[1] x[2][2] b = [3, 1] x = [1, [2, 1], [1, 4, [3, 1]], 5] c = [1, 4, b] x = [1, a, c, 5] x[0] x[1][1] x[2][0] x[2][2][1] 10/5/17 Nested Lists and Dictionaries 5
Two Dimensional Lists Table of Data Images 0 1 2 3 0 1 2 3 4 5 6 7 8 9 101112 0 Each row, col 0 5 4 7 3 1 2 has a value 3 Each row, col has 1 4 8 9 7 4 5 an RGB value 2 5 1 2 3 6 7 8 3 4 1 2 9 9 10 11 4 6 7 8 0 12 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]] 10/5/17 Nested Lists and Dictionaries 6
Overview of Two-Dimensional Lists • Access value at row 3, col 2: 0 1 2 3 d[3][2] d 5 4 7 3 0 1 4 8 9 7 • Assign value at row 3, col 2: 5 1 2 3 2 d[3][2] = 8 4 1 2 9 3 6 7 8 0 4 • An odd symmetry § Number of rows of d: len(d) § Number of cols in row r of d: len(d[r]) 10/5/17 Nested Lists and Dictionaries 7
How Multidimensional Lists are Stored • b = [[9, 6, 4], [5, 7, 7]] 9 6 4 id2 id3 id1 5 7 7 9 5 id2 6 7 id3 id1 b 4 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/5/17 Nested Lists and Dictionaries 8
Image Data: 2D Lists of Pixels 0 1 2 3 4 5 6 7 8 9 101112 id23 0 1 RGB 2 3 4 b[0][0] is a red 255 5 6 white pixel 7 green 255 8 9 10 blue 255 11 12 id1 id2 id1 b list list id2 id23 id3 id24 … … 10/5/17 Nested Lists and Dictionaries 9
Ragged Lists: Rows w/ Different Length • b = [[17,13,19],[28,95]] id2 id1 id3 id1 0 17 b 0 0 id2 28 1 13 id3 95 1 1 2 19 • Will see applications of this later 10/5/17 Nested Lists and Dictionaries 10
Slices and Multidimensional Lists • Only “top-level” list is copied. x = b[:2] • Contents of the list are not altered • b = [[9, 6], [4, 5], [7, 7]] x id5 b id1 id2 id5 9 id2 id1 6 id3 id3 id2 id3 4 id4 5 id4 7 7 10/5/17 Nested Lists and Dictionaries 11
Slices and Multidimensional Lists • Only “top-level” list is copied. x = b[:2] • Contents of the list are not altered • b = [[9, 6], [4, 5], [7, 7]] x id5 b id1 id2 id5 9 id2 id1 6 id3 id3 id2 id3 4 id4 5 id4 7 7 10/5/17 Nested Lists and Dictionaries 12
Slices and Multidimensional Lists • What are the contents of • Create a nested list the list (with name) in b ? >>> b = [[9,6],[4,5],[7,7]] • Get a slice A: [[9,6],[4,5],[7,7]] >>> x = b[:2] B: [[9,6],[4,5,10]] • Append to a row of x C: [[9,6],[4,5,10],[7,7]] >>> x[1].append(10) D: [[9,6],[4,10],[7,7]] E: I don’t know • x now has nested list [[9, 6], [4, 5, 10]] 10/5/17 Nested Lists and Dictionaries 13
Slices and Multidimensional Lists • What are the contents of • Create a nested list the list (with name) in b ? >>> b = [[9,6],[4,5],[7,7]] • Get a slice A: [[9,6],[4,5],[7,7]] >>> x = b[:2] B: [[9,6],[4,5,10]] • Append to a row of x C: [[9,6],[4,5,10],[7,7]] >>> x[1].append(10) D: [[9,6],[4,10],[7,7]] E: I don’t know • x now has nested list [[9, 6], [4, 5, 10]] 10/5/17 Nested Lists and Dictionaries 14
Functions and 2D Lists def transpose(table): """Returns: copy of table with rows and columns swapped 1 2 Precondition: table is a (non-ragged) 2d List""" 3 4 numrows = len(table) # Need number of rows numcols = len(table[0]) # All rows have same no. cols 5 6 result = [] # Result (new table) accumulator for m in range(numcols): # Get the column elements at position m # Make a new list for this column 1 3 5 # Add this row to accumulator table 2 4 6 return result 10/5/17 Nested Lists and Dictionaries 15
Functions and 2D Lists def transpose(table): """Returns: copy of table with rows and columns swapped 1 2 Precondition: table is a (non-ragged) 2d List""" 3 4 numrows = len(table) # Need number of rows numcols = len(table[0]) # All rows have same no. cols 5 6 result = [] # Result (new table) accumulator for m in range(numcols): row = [] # Single row accumulator for n in range(numrows): 1 3 5 row.append(table[n][m]) # Create a new row list 2 4 6 result.append(row) # Add result to table return result 10/5/17 Nested Lists and Dictionaries 16
Functions and 2D Lists def transpose(table): """Returns: copy of table with rows and columns swapped 1 2 Precondition: table is a (non-ragged) 2d List""" 3 4 numrows = len(table) # Need number of rows numcols = len(table[0]) # All rows have same no. cols 5 6 result = [] # Result (new table) accumulator for m in range(numcols): Nest lists need row = [] # Single row accumulator nested loops for n in range(numrows): 1 3 5 row.append(table[n][m]) # Create a new row list 2 4 6 result.append(row) # Add result to table return result 10/5/17 Nested Lists and Dictionaries 17
Dictionaries (Type dict ) Description Python Syntax • Create with format: • List of key-value pairs {k1:v1, k2:v2, …} § Keys are unique • Keys must be non-mutable § Values need not be § ints, floats, bools, strings • Example: net-ids § Not lists or custom objects § net-ids are unique (a key) • Values can be anything § names need not be (values) • Example: § js1 is John Smith (class ’13) d = {'js1':'John Smith', § js2 is John Smith (class ’16) 'js2':'John Smith', • Many other applications 'wmw2':'Walker White'} 10/5/17 Nested Lists and Dictionaries 18
Using Dictionaries (Type dict ) d = {'js1':'John','js2':'John', • Access elts. like a list 'wmw2':'Walker'} § d['js1'] evaluates to 'John' § But cannot slice ranges! d id8 id8 • Dictionaries are mutable dict § Can reassign values 'js1' 'John' § d['js1'] = 'Jane' 'js2' 'John' § Can add new keys 'wmw2' 'Walker' § d['aa1'] = 'Allen' § Can delete keys Key-Value order in § del d['wmw2'] folder is not important 10/5/17 Nested Lists and Dictionaries 19
Using Dictionaries (Type dict ) d = {'js1':'John','js2':'John', • Access elts. like a list 'wmw2':'Walker'} § d['js1'] evaluates to 'John' § But cannot slice ranges! d id8 id8 • Dictionaries are mutable dict § Can reassign values ✗ 'js1' 'John' 'Jane' § d['js1'] = 'Jane' 'js2' 'John' § Can add new keys 'wmw2' 'Walker' § d['aa1'] = 'Allen' § Can delete keys Key-Value order in § del d['wmw2'] folder is not important 10/5/17 Nested Lists and Dictionaries 20
Using Dictionaries (Type dict ) d = {'js1':'John','js2':'John', • Access elts. like a list 'wmw2':'Walker'} § d['js1'] evaluates to 'John' § But cannot slice ranges! d id8 id8 • Dictionaries are mutable dict § Can reassign values 'js1' 'Jane' § d['js1'] = 'Jane' 'js2' 'John' § Can add new keys 'wmw2' 'Walker' § d['aa1'] = 'Allen' 'aa1' 'Allen' § Can delete keys § del d['wmw2'] 10/5/17 Nested Lists and Dictionaries 21
Using Dictionaries (Type dict ) d = {'js1':'John','js2':'John', • Access elts. like a list 'wmw2':'Walker'} § d['js1'] evaluates to 'John' § But cannot slice ranges! d id8 id8 • Dictionaries are mutable dict § Can reassign values 'js1' 'Jane' § d['js1'] = 'Jane' 'js2' 'John' § Can add new keys ✗ ✗ 'wmw2' 'Walker' § d['aa1'] = 'Allen' 'aa1' 'Allen' § Can delete keys § del d['wmw2'] Deleting key deletes both 10/5/17 Nested Lists and Dictionaries 22
Recommend
More recommend