lecture 13 nested lists tuples and dictionaries
play

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

http://www.cs.cornell.edu/courses/cs1110/2019sp Lecture 13: 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/2019sp Lecture 13: 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. Nested Lists • Lists can hold any objects • Lists are objects • Therefore lists can hold other lists! x[2] b = [3, 1] x[2][2] x[1] c = [1, 4, b] x = [1, [2, 1], [1, 4, [3, 1]], 5] a = [2, 1] x = [1, a, c, 5] x[2][0] x[2][2][0] x[0] x[1][1] 2

  3. 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 5 1 2 3 6 2 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]] 3

  4. Overview of Two-Dimensional Lists 0 1 2 3 5 4 7 3 0 1 4 8 9 7 5 1 2 3 2 4 1 2 9 3 >>> d = [[5,4,7,3],[4,8,9,7],[5,1,2,3],[4,1,2,9]] Access value at row 3, col 2 >>> d[3][2] 2 Assign value at row 3, col 2 >>> d[3][2] = 8 Number of rows of d >>> len(d) 4 Number of cols in row 2 of d >>> len(d[2]) 4 >>> d [[5, 4, 7, 3], [4, 8, 9, 7],[5, 1, 2, 3], [4, 1, 8, 9]]

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

  6. Ragged Lists: Rows w/ Different Length • b = [[17,13,19],[28,95]] Heap Space Global Space id2 id1 id3 id1 0 17 b 0 id2 0 28 1 13 1 id3 1 95 19 2 6

  7. Slices and Multidimensional Lists • Only “top-level” list is copied. • Contents of the list are not altered id2 9 b = [[9, 6], [4, 5], [7, 7]] 0 6 1 Heap Space Global Space id1 id1 b id3 0 id2 0 4 id5 x 1 id3 1 5 2 id4 x = b[:2] id4 id5 7 0 id2 0 7 1 id3 7 1

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

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

  10. 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]] 10

  11. 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]] 11

  12. 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 • 12

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

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

  15. 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.”) 15

  16. Dictionaries (Type dict ) Description Python Syntax • Create with format: • List of key-value pairs {key1:value1, key2:value2, …} § 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', 'tm55':'Toni Morrison'} 16

  17. Using Dictionaries (Type dict ) >>> d = {'ec1':'Ezra', 'ec2':'Ezra', 'tm55':'Toni'} >>> d['ec1'] 'Ezra' Global Space >>> d[0] Traceback (most recent call last): d id8 File "<stdin>", line 1, in <module> KeyError: 0 Heap Space >>> d[:1] Traceback (most recent call last): id8 File "<stdin>", line 1, in <module> dict TypeError: unhashable type: 'slice' 'Ezra' 'ec1' >>> 'Ezra' 'ec2' • Can access elements like a list 'Toni' 'tm55' • Must use the key, not an index 17 • Cannot slice ranges

  18. Using Dictionaries (Type dict ) d = {'ec1':'Ezra','ec2':'Ezra', • Dictionaries are mutable 'tm55':'Toni'} § Can reassign values § d['ec1'] = 'Ellis' d id8 id8 dict 'Ezra' 'ec1' 'Ezra' 'ec2' 'Toni' 'tm55' 18

  19. Using Dictionaries (Type dict ) d = {'ec1':'Ezra','ec2':'Ezra', • Dictionaries are mutable 'tm55':'Toni'} § Can reassign values § d['ec1'] = 'Ellis' d id8 id8 dict 'ec1' û 'Ezra' 'Ellis' 'Ezra' 'ec2' 'Toni' 'tm55' 19

  20. Using Dictionaries (Type dict ) d = {'ec1':'Ezra','ec2':'Ezra', • Dictionaries are mutable 'tm55':'Toni'} § Can reassign values § d['ec1'] = 'Ellis' d id8 § Can add new keys id8 dict § d['psb26'] = 'Pearl' 'ec1' û 'Ezra' 'Ellis' 'Ezra' 'ec2' 'Toni' 'tm55' 20

  21. Using Dictionaries (Type dict ) d = {'ec1':'Ezra','ec2':'Ezra', • Dictionaries are mutable 'tm55':'Toni','psb26':'Pearl'} § Can reassign values § d['ec1'] = 'Ellis' d id8 § Can add new keys id8 dict § d['psb26'] = 'Pearl' 'ec1' û 'Ezra' 'Ellis' 'Ezra' 'ec2' 'Toni' 'tm55' 'Pearl' 'psb26' 21

  22. Using Dictionaries (Type dict ) d = {'ec1':'Ezra','ec2':'Ezra', • Dictionaries are mutable 'tm55':'Toni','psb26':'Pearl'} § Can reassign values § d['ec1'] = 'Ellis' d id8 § Can add new keys id8 dict § d['psb26'] = 'Pearl' 'ec1' û 'Ezra' 'Ellis' § Can delete keys 'Ezra' 'ec2' § del d['tm55'] 'Toni' 'tm55' 'Pearl' 'psb26' 22

  23. Using Dictionaries (Type dict ) d = {'ec1':'Ezra','ec2':'Ezra', • Dictionaries are mutable 'psb26':'Pearl'} § Can reassign values § d['ec1'] = 'Ellis' d id8 § Can add new keys id8 dict § d['psb26'] = 'Pearl' 'ec1' û 'Ezra' 'Ellis' § Can delete keys 'Ezra' 'ec2' § del d['tm55'] û û 'Erik' 'tm55' 'Pearl' 'psb26' Deleting key deletes both 23

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend