nested lists and dictionaries
play

Nested Lists and Dictionaries [Andersen, Gries, Lee, Marschner, Van - PowerPoint PPT Presentation

CS 1110: Introduction to Computing Using Python Lecture 14 Nested Lists and Dictionaries [Andersen, Gries, Lee, Marschner, Van Loan, White] Announcements Prelim = not Thursday or Friday might be on weekend or next week Dean of


  1. CS 1110: Introduction to Computing Using Python Lecture 14 Nested Lists and Dictionaries [Andersen, Gries, Lee, Marschner, Van Loan, White]

  2. Announcements • Prelim = not Thursday or Friday • might be on weekend or next week • Dean of Faculty supports our request to extend the drop deadline (but no official decision yet) 3/16/17 Nested Lists and Dictionaries 2

  3. Next week: Recursion • Tuesday and Thursday: Recursion. • Reading: 5.8-5.10 3/16/17 Nested Lists and Dictionaries 3

  4. Announcements: Lab 8 • Lab 8 has been released • Could be useful as extra for loop practice. • Prelim does not depend on any material introduced in this lab. 3/16/17 Nested Lists and Dictionaries 4

  5. Nested Lists • Lists can hold any objects • Lists are objects • Therefore lists can hold other lists! x[2] b = [3, 1] x[1] x[2][2] c = [1, 4, b] x = [1, [2, 1], [1, 4, [3, 1]], 5] a = [2, 1] x = [1, a, c, 5] x[1][1] x[2][0] x[0] x[2][2][0] 3/16/17 Nested Lists and Dictionaries 5

  6. 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 6 2 5 1 2 3 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]] 3/16/17 Nested Lists and Dictionaries 6

  7. 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 • Number of rows of d: 6 7 8 0 4  len(d) • Number of cols in row r of d:  len(d[r]) 3/16/17 Nested Lists and Dictionaries 7

  8. How Multidimensional Lists are Stored • b = [[9, 6, 4], [5, 7, 7]] id2 id3 9 6 4 id1 5 7 7 0 5 0 9 0 id2 1 7 1 6 1 id3 id1 b 2 2 7 4 • b holds id of a one-dimensional list  Has len(b) elements • b[i] holds id of a one-dimensional list  Has len(b[i]) elements 3/16/17 Nested Lists and Dictionaries 8

  9. Ragged Lists: Rows w/ Different Length • b = [[17,13,19],[28,95]] id2 id1 id3 id1 0 b 17 0 id2 0 28 1 13 1 id3 1 95 19 2 3/16/17 Nested Lists and Dictionaries 9

  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 0 9 0 id2 id1 1 6 1 id3 id3 0 id2 1 id3 0 4 2 id4 1 5 id4 0 7 1 7 3/16/17 Nested Lists and Dictionaries 10

  11. Slices and Multidimensional Lists • What is now in x ? • Create a nested list >>> b = [[9,6],[4,5],[7,7]] • Get a slice A: [[9,6,10]] >>> 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 3/16/17 Nested Lists and Dictionaries 11

  12. Slices and Multidimensional Lists • What is now in b ? • Create a nested list >>> 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]] 3/16/17 Nested Lists and Dictionaries 12

  13. Data Wrangling: Transpose 1 2 3 4 5 6 1 3 5 2 4 6 3/16/17 Nested Lists and Dictionaries 13

  14. Data Wrangling: Transpose 1 2 3 4 5 6 1 3 5 2 4 6 3/16/17 Nested Lists and Dictionaries 14

  15. Data Wrangling: Transpose 1 2 3 4 5 6 1 3 5 2 4 6 3/16/17 Nested Lists and Dictionaries 15

  16. Data Wrangling: Transpose 1 2 3 4 5 6 1 3 5 2 4 6 3/16/17 Nested Lists and Dictionaries 16

  17. Data Wrangling: Transpose 1 2 3 4 5 6 1 3 5 2 4 6 3/16/17 Nested Lists and Dictionaries 17

  18. Data Wrangling: Transpose 1 2 3 4 5 6 1 3 5 2 4 6 3/16/17 Nested Lists and Dictionaries 18

  19. Data Wrangling: Transpose 1 2 3 4 5 6 1 3 5 2 4 6 3/16/17 Nested Lists and Dictionaries 19

  20. Data Wrangling: Transpose 1 2 3 4 5 6 1 3 5 2 4 6 3/16/17 Nested Lists and Dictionaries 20

  21. Data Wrangling: Transpose 1 2 3 4 5 6 1 3 5 2 4 6 3/16/17 Nested Lists and Dictionaries 21

  22. Data Wrangling: Transpose 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) numcols = len(table[0]) # All rows have same no. cols 5 6 result = [] # Result accumulator for m in range(numcols): row = [] # Single row accumulator for n in range(numrows): 1 3 5 row.append(table[n][m]) # Build up row result.append(row) # Add result to table 2 4 6 return result 3/16/17 Nested Lists and Dictionaries 22

  23. Data Wrangling 3 1 2 0 0 1 2 3 3/16/17 Nested Lists and Dictionaries 23

  24. Dictionaries (Type dict ) Description Python Syntax • Create with format: • List of key-value pairs {k1:v1, k2:v2, …}  Keys are unique • Keys must be immutable  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 = {'ec1':'Ezra Cornell',  js2 is John Smith (class ’16) 'ec2':'Ezra Cornell', 'ela63':'Erik Andersen'} 3/16/17 Nested Lists and Dictionaries 25

  25. Using Dictionaries (Type dict ) d = {'ec1':'Ezra','ec2':'Ezra', • Access elts. like a list 'ela63':'Erik'}  d['ec1'] evaluates to 'Ezra'  But cannot slice ranges! d id8 id8 dict 'ec1' 'Ezra' 'Ezra' 'ec2' 'Erik' 'ela63' 3/16/17 Nested Lists and Dictionaries 26

  26. Using Dictionaries (Type dict ) d = {'ec1':'Ezra','ec2':'Ezra', • Dictionaries are mutable 'ela63':'Erik'}  Can reassign values  d['ec1'] = 'Ellis' d id8 id8 dict 'ec1' 'Ezra' 'Ezra' 'ec2' 'Erik' 'ela63' 3/16/17 Nested Lists and Dictionaries 27

  27. Using Dictionaries (Type dict ) d = {'ec1':'Ezra','ec2':'Ezra', • Dictionaries are mutable 'ela63':'Erik'}  Can reassign values  d['ec1'] = 'Ellis' d id8 id8 dict 'ec1'  'Ezra' 'Ellis' 'Ezra' 'ec2' 'Erik' 'ela63' 3/16/17 Nested Lists and Dictionaries 28

  28. Using Dictionaries (Type dict ) d = {'ec1':'Ezra','ec2':'Ezra', • Dictionaries are mutable 'ela63':'Erik'}  Can reassign values  d['ec1'] = 'Ellis' d id8  Can add new keys id8  d['aa1'] = 'Allen' dict 'ec1'  'Ezra' 'Ellis' 'Ezra' 'ec2' 'Erik' 'ela63' 3/16/17 Nested Lists and Dictionaries 29

  29. Using Dictionaries (Type dict ) d = {'ec1':'Ezra','ec2':'Ezra', • Dictionaries are mutable 'ela63':'Erik','aa1':'Allen'}  Can reassign values  d['ec1'] = 'Ellis' d id8  Can add new keys id8  d['aa1'] = 'Allen' dict 'ec1'  'Ezra' 'Ellis' 'Ezra' 'ec2' 'Erik' 'ela63' 'Allen' 'aa1' 3/16/17 Nested Lists and Dictionaries 30

  30. Using Dictionaries (Type dict ) d = {'ec1':'Ezra','ec2':'Ezra', • Dictionaries are mutable 'ela63':'Erik','aa1':'Allen'}  Can reassign values  d['ec1'] = 'Ellis' d id8  Can add new keys id8  d['aa1'] = 'Allen' dict 'ec1'  'Ezra' 'Ellis'  Can delete keys 'Ezra' 'ec2'  del d['ela63'] 'Erik' 'ela63' 'Allen' 'aa1' 3/16/17 Nested Lists and Dictionaries 31

  31. Using Dictionaries (Type dict ) d = {'ec1':'Ezra','ec2':'Ezra', • Dictionaries are mutable 'aa1':'Allen'}  Can reassign values  d['ec1'] = 'Ellis' d id8  Can add new keys id8  d['aa1'] = 'Allen' dict 'ec1'  'Ezra' 'Ellis'  Can delete keys 'Ezra' 'ec2'  del d['ela63']   'Erik' 'ela63' 'Allen' 'aa1' Deleting key deletes both 3/16/17 Nested Lists and Dictionaries 32

  32. New Way to do Test Cases test_cases = {4:3, 5:3, 6:6, -2:0, 20:7} for item in test_cases: seq = orig[:] # this creates a copy of the list orig print "\tTesting input " + str(seq) + ", " + str(item) output = lab08.lesser_than(seq, item) ct.assert_equals(test_cases[item], output) ct.assert_equals(seq, orig) # make sure argument list wasn't changed 3/16/17 Nested Lists and Dictionaries 33

Recommend


More recommend