lecture 14 nested lists tuples and dictionaries
play

Lecture 14: Nested Lists, Tuples, and Dictionaries (Sections - PowerPoint PPT Presentation

http://www.cs.cornell.edu/courses/cs1110/2018sp Lecture 14: Nested Lists, Tuples, and Dictionaries (Sections 11.1-11.5, 12.1-12) CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Gries, L. Lee, S. Marschner, C. Van


  1. http://www.cs.cornell.edu/courses/cs1110/2018sp Lecture 14: Nested Lists, Tuples, and Dictionaries (Sections 11.1-11.5, 12.1-12) CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Gries, L. Lee, S. Marschner, C. Van Loan, W. White]

  2. Announcements • A3 Tentative release date: Mon Mar 19-Thu Mar 22; tentative time for completion: somewhere between 1 and 2 weeks. Similar to A3 from Spring 2017. • Prelim 1 Grading this weekend. Grades will come out before the drop deadline. 2

  3. Next week: Recursion • Tuesday and Thursday: Recursion. • Reading: 5.8-5.10 3

  4. 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[2][2][0] x[0] 4

  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 an RGB value 5 2 5 1 2 3 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]] 5

  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 • Number of rows of d: 6 7 8 0 4 § len(d) • Number of cols in row r of d: § len(d[r]) 6

  7. How Multidimensional Lists are Stored • b = [[9, 6, 4], [5, 7, 7]] id2 id3 9 6 4 id1 5 7 7 5 0 0 9 0 id2 7 1 6 1 1 id3 id1 2 b 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 7

  8. 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 1 1 id3 95 19 2 8

  9. 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 9

  10. Slices & Multidimensional Lists (Q1) • 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 10

  11. Slices & Multidimensional Lists (A1) • 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 11

  12. Slices & Multidimensional Lists (Q2) • 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]] 12

  13. Slices & Multidimensional Lists (A2) • 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]] 13

  14. Data Wrangling: Transpose Idea 1 2 1 3 5 7 3 4 2 4 6 8 5 6 7 8 4 lists: 2 elements in each 2 lists: 4 elements in each How to transpose? 1 st element of each list gets appended to 1 st list • 2 nd element of each list gets appended to 2 nd list • 14

  15. Data Wrangling: Transpose Code def transpose(orig_table): """Returns: copy of table with rows and columns swapped 1 2 Precondition: table is a (non-ragged) 2d List""" 3 4 numrows = len(orig_table) numcols = len(orig_table[0]) # All rows have same no. cols 5 6 new_table = [] # Result accumulator for m in list(range(numcols)): row = [] # Single row accumulator for n in list(range(numrows)): 1 3 5 row.append(old_table[n][m]) # Build up new row new_table.append(row) # Add new row to new table 2 4 6 return new_table 15

  16. Tuples strings: lists: immutable sequences of characters mutable sequences of any objects “tuple” generalizes “pair,” “triple,” “quadruple,” … tuples: immutable sequences of any objects • Tuples fall between strings and lists § write them with just commas: 42, 4.0, ‘x’ § often enclosed in parentheses: ( 42, 4.0, ‘x’) Conventionally use tuples for: Conventionally use lists for: • short sequences • long sequences • heterogeneous sequences • homogeneous sequences • fixed length sequences • variable length sequences 16

  17. Returning multiple values • Can use lists/tuples to return multiple values INCHES_PER_FOOT = 12 def to_feet_and_inches(height_in_inches): feet = height_in_inches // INCHES_PER_FOOT inches = height_in_inches % INCHES_PER_FOOT return (feet, inches) all_inches = 68 (ft,ins) = to_feet_and_inches(all_inches) print(You are “+str(ft)+” feet, “+str(ins)+” inches.”) 17

  18. 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'} 18

  19. Using Dictionaries (Type dict ) d = {'ec1':'Ezra','ec2':'Ezra', • Access elements like a list 'ela63':'Erik'} § d['ec1'] evaluates to 'Ezra' § But cannot slice ranges! d id8 id8 dict 'ec1' 'Ezra' 'Ezra' 'ec2' 'ela63' 'Erik' 19

  20. 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' 'ela63' 'Erik' 20

  21. 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' 'ela63' 'Erik' 21

  22. 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 dict § d['aa1'] = 'Allen' 'ec1' û 'Ezra' 'Ellis' 'Ezra' 'ec2' 'ela63' 'Erik' 22

  23. 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 dict § d['aa1'] = 'Allen' 'ec1' û 'Ezra' 'Ellis' 'Ezra' 'ec2' 'ela63' 'Erik' 'Allen' 'aa1' 23

  24. 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 dict § d['aa1'] = 'Allen' 'ec1' û 'Ezra' 'Ellis' § Can delete keys 'Ezra' 'ec2' § del d['ela63'] 'ela63' 'Erik' 'Allen' 'aa1' 24

  25. 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 dict § d['aa1'] = 'Allen' 'ec1' û 'Ezra' 'Ellis' § Can delete keys 'Ezra' 'ec2' § del d['ela63'] û û 'ela63' 'Erik' 'Allen' 'aa1' Deleting key deletes both 25

Recommend


More recommend