nested lists and dictionaries announcements for this
play

Nested Lists and Dictionaries Announcements for This Lecture - PowerPoint PPT Presentation

Lecture 16 Nested Lists and Dictionaries Announcements for This Lecture Prelim and Regrades Assignments/Reading Regrades are now open Should be working on A4 Only for MAJOR mistakes Tasks 1-2 by tomorrow You might lose points


  1. Lecture 16 Nested Lists and Dictionaries

  2. Announcements for This Lecture Prelim and Regrades Assignments/Reading • Regrades are now open • Should be working on A4 § Only for MAJOR mistakes § Tasks 1-2 by tomorrow § You might lose points § Task 3 by the weekend § Recursion next week • The regrade process • Reading : Chapters 15, 16 § Ask in Gradescope § Chapter 17 for next week § Tell us what to look for § Lot of potential reading § If valid, we will respond § … but we are covering a lot § We will also update CMS 10/22/19 Nested Lists and Dictionaries 2

  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 • Folders linking to folders x[2] x id13 id12 § Top folder for the list id12 § Other folders for contents id10 id11 RGB RGB • Example: RGB red 0 red 255 >>> r = introcs.RGB(255,0,0) red 0 green 255 green 0 >>> b = introcs.RGB(0,0,255) green 0 >>> g = introcs.RGB(0,255,0) blue 0 blue 0 blue 255 >>> x = [r,b,g] 10/22/19 Nested Lists and Dictionaries 3

  4. 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 • Folders linking to folders x[2] x id13 id12 § Top folder for the list id12 § Other folders for contents id10 id11 RGB RGB • Example: RGB red 0 red 255 >>> r = introcs.RGB(255,0,0) red 0 green 255 green 0 >>> b = introcs.RGB(0,0,255) green 0 >>> g = introcs.RGB(0,255,0) blue 0 blue 0 blue 255 >>> x = [r,b,g] 10/22/19 Nested Lists and Dictionaries 4

  5. 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/22/19 Nested Lists and Dictionaries 5

  6. 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 two-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/22/19 Nested Lists and Dictionaries 6

  7. Ragged Lists vs Tables • Ragged is 2d uneven list: 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 • Table is 2d uniform list: 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 10/22/19 Nested Lists and Dictionaries 7

  8. Nested Lists can Represent Tables Spreadsheet Image 0 1 2 3 4 5 6 7 8 9 101112 0 1 2 3 0 1 0 5 4 7 3 2 3 1 4 8 9 7 4 5 6 2 5 1 2 3 7 8 9 3 4 1 2 9 10 11 12 4 6 7 8 0 table.csv smile.xlsx 10/22/19 Nested Lists and Dictionaries 8

  9. Representing Tables as Lists • Represent as 2d list Spreadsheet § Each table row a list 0 1 2 3 § List of all rows Each row, 0 5 4 7 3 § Row major order col has a 1 4 8 9 7 value • Column major exists 2 5 1 2 3 § Less common to see 3 4 1 2 9 § Limited to some 4 6 7 8 0 scientific applications d = [[5,4,7,3],[4,8,9,7],[5,1,2,3],[4,1,2,9],[6,7,8,0]] 10/22/19 Nested Lists and Dictionaries 9

  10. Image Data: 2D Lists of Pixels 0 1 2 3 4 5 6 7 8 9 101112 id23 0 1 RGB 2 3 4 red 255 smile.py 5 6 7 green 255 8 9 10 blue 255 11 12 id1 id2 id1 b list list id2 id23 id3 id24 … … 10/22/19 Nested Lists and Dictionaries 10

  11. 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/22/19 Nested Lists and Dictionaries 11

  12. 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/22/19 Nested Lists and Dictionaries 12

  13. 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/22/19 Nested Lists and Dictionaries 13

  14. 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/22/19 Nested Lists and Dictionaries 14

  15. 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/22/19 Nested Lists and Dictionaries 15

  16. Shallow vs. Deep Copy • Shallow copy: Copy top-level list § Happens when slice a multidimensional list • Deep copy: Copy top and all nested lists § Requires a special function: copy.deepcopy • Example: >>> import copy >>> a = [[1,2],[2,3]] >>> b = a[:] # Shallow copy >>> c = copy.deepcopy(a) # Deep copy 10/22/19 Nested Lists and Dictionaries 16

  17. Functions over Nested Lists • Functions on nested lists similar to lists § Go over (nested) list with for-loop § Use accumulator to gather the results • But two important differences § Need multiple for-loops § One for each part/dimension of loop § In some cases need multiple accumulators § Latter true when result is new table 10/22/19 Nested Lists and Dictionaries 17

  18. Simple Example def all_nums(table): """Returns True if table contains only numbers Precondition: table is a (non-ragged) 2d List""" result = True Accumulator # Walk through table for row in table: First Loop # Walk through the row Second Loop for item in row: if not type(item) in [int,float]: result = False return result 10/22/19 Nested Lists and Dictionaries 18

  19. Transpose: A Trickier Example def transpose(table): """Returns: copy of table with rows and columns swapped 1 2 Precondition: table is a (non-ragged) 2d List""" 3 4 5 6 result = [] # Result (new table) accumulator # Loop over columns # Add each column as a ROW to result 1 3 5 2 4 6 return result 10/22/19 Nested Lists and Dictionaries 19

  20. Transpose: A Trickier Example 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/22/19 Nested Lists and Dictionaries 20

  21. Transpose: A Trickier Example 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/22/19 Nested Lists and Dictionaries 21

  22. Transpose: A Trickier Example 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 Accumulator Accumulator for m in range(numcols): for each loop for each loop 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/22/19 Nested Lists and Dictionaries 22

Recommend


More recommend