Mini-Lecture 16 Nested Lists
Nested Lists • Lists can hold any object • Lists are themselves 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/18 Nested Lists 2
Two Dimensional Lists Table of Data Images 0 1 2 3 0 1 2 3 4 5 6 7 8 9 101112 0 5 4 7 3 Each row, col 0 1 2 has a value 3 Each row, col has 4 8 9 7 1 4 an RGB value 5 5 1 2 3 2 6 7 8 4 1 2 9 3 9 10 11 6 7 8 0 4 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/18 Nested Lists 3
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/18 Nested Lists 4
How Multidimensional Lists are Stored • b = [[9, 6, 4], [5, 7, 7]] id2 id3 9 6 4 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/18 Nested Lists 5
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 1 95 1 2 19 • Will see applications of this later 10/5/18 Nested Lists 6
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/18 Nested Lists 7
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/18 Nested Lists 8
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]] D: [[9,6],[4,10],[7,7]] >>> x[1].append(10) E: I don’t know • x now has nested list [[9, 6], [4, 5, 10]] 10/5/18 Nested Lists 9
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]] D: [[9,6],[4,10],[7,7]] >>> x[1].append(10) E: I don’t know • x now has nested list [[9, 6], [4, 5, 10]] 10/5/18 Nested Lists 10
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/18 Nested Lists 11
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/18 Nested Lists 12
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/18 Nested Lists 13
JSON: Mixing and Lists and Dictionaries { • weather.json : Nested "wind" : { Dictionary § Weather measurements "speed" : 13.0, at Ithaca Airport (2017) "crosswind" : 5.0 }, § Keys : Times (Each hour) Nested "sky" : [ List § Values : Weather readings { "cover" : "clouds", • This is a nested JSON "type" : "broken", "height" : 1200.0 § Values are also dictionaries }, § Containing more dictionaries { "type" : "overcast", § And also containing lists "height" : 1800.0 } Nested See weather.py ] Dictionary } 10/5/18 Nested Lists 14
Recommend
More recommend