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

lecture 14 nested lists tuples and dictionaries
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

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]

http://www.cs.cornell.edu/courses/cs1110/2018sp

slide-2
SLIDE 2
  • 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.

Announcements

2

slide-3
SLIDE 3
  • Tuesday and Thursday: Recursion.
  • Reading: 5.8-5.10

Next week: Recursion

3

slide-4
SLIDE 4

Nested Lists

  • Lists can hold any objects
  • Lists are objects
  • Therefore lists can hold other lists!

x = [1, [2, 1], [1, 4, [3, 1]], 5]

x[0] x[1][1] x[2][2][0] x[2][0] x[1] x[2] x[2][2]

b = [3, 1] c = [1, 4, b] a = [2, 1] x = [1, a, c, 5]

4

slide-5
SLIDE 5

Two Dimensional Lists

Table of Data Images

5

5 4 7 3 4 8 9 7 5 1 2 3 4 1 2 9 6 7 8 0 0 1 2 3 1 4 2 3

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

0 1 2 3 4 5 6 7 8 9 101112 1 2 3 4 5 6 7 8 9 10 11 12

Each row, col has a value Each row, col has an RGB value

slide-6
SLIDE 6

Overview of Two-Dimensional Lists

  • Access value at row 3, col 2:

d[3][2]

  • Assign value at row 3, col 2:

d[3][2] = 8

  • Number of rows of d:

§ len(d)

  • Number of cols in row r of d:

§ len(d[r])

6

5 4 7 3 4 8 9 7 5 1 2 3 4 1 2 9 6 7 8 0 d 0 1 2 3 1 4 2 3

slide-7
SLIDE 7

How Multidimensional Lists are Stored

  • b = [[9, 6, 4], [5, 7, 7]]
  • 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

id2

9 6 4

id3

5 7 7

id1 id2 id3 id1 b

9 6 4 5 7 7

1 1 2 1 2

slide-8
SLIDE 8

Ragged Lists: Rows w/ Different Length

8

  • b = [[17,13,19],[28,95]]

id2

17 13 19

id3

28 95

id1 id1 b id2 id3

1 1 2 1

slide-9
SLIDE 9

Slices and Multidimensional Lists

  • Only “top-level” list is copied.
  • Contents of the list are not altered
  • b = [[9, 6], [4, 5], [7, 7]]

9

id2

9 6

id1 id2 id3 id1

b

id4 id3

4 5

id4

7 7

x = b[:2]

id5

x

id5 id2 id3

1 1 1 1 1 2

slide-10
SLIDE 10

Slices & Multidimensional Lists (Q1)

  • Create a nested list

>>> b = [[9,6],[4,5],[7,7]]

  • Get a slice

>>> x = b[:2]

  • Append to a row of x

>>> x[1].append(10)

  • What is now in x?

10

A: [[9,6,10]] B: [[9,6],[4,5,10]] C: [[9,6],[4,5,10],[7,7]] D: [[9,6],[4,10],[7,7]] E: I don’t know

slide-11
SLIDE 11

Slices & Multidimensional Lists (A1)

  • Create a nested list

>>> b = [[9,6],[4,5],[7,7]]

  • Get a slice

>>> x = b[:2]

  • Append to a row of x

>>> x[1].append(10)

  • What is now in x?

11

A: [[9,6,10]] B: [[9,6],[4,5,10]] C: [[9,6],[4,5,10],[7,7]] D: [[9,6],[4,10],[7,7]] E: I don’t know

slide-12
SLIDE 12

Slices & Multidimensional Lists (Q2)

  • Create a nested list

>>> b = [[9,6],[4,5],[7,7]]

  • Get a slice

>>> x = b[:2]

  • Append to a row of x

>>> x[1].append(10)

  • x now has nested list

[[9, 6], [4, 5, 10]]

  • What is now in b?

12

A: [[9,6],[4,5],[7,7]] B: [[9,6],[4,5,10]] C: [[9,6],[4,5,10],[7,7]] D: [[9,6],[4,10],[7,7]] E: I don’t know

slide-13
SLIDE 13

Slices & Multidimensional Lists (A2)

  • Create a nested list

>>> b = [[9,6],[4,5],[7,7]]

  • Get a slice

>>> x = b[:2]

  • Append to a row of x

>>> x[1].append(10)

  • x now has nested list

[[9, 6], [4, 5, 10]]

  • What is now in b?

13

A: [[9,6],[4,5],[7,7]] B: [[9,6],[4,5,10]] C: [[9,6],[4,5,10],[7,7]] D: [[9,6],[4,10],[7,7]] E: I don’t know

slide-14
SLIDE 14

Data Wrangling: Transpose Idea

14

1 3 5 7 2 4 6 8 1 2 3 4 5 6 7 8

4 lists: 2 elements in each How to transpose?

  • 1st element of each list gets appended to 1st list
  • 2nd element of each list gets appended to 2nd list

2 lists: 4 elements in each

slide-15
SLIDE 15

Data Wrangling: Transpose Code

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

15

1 2 3 4 5 6 1 3 5 2 4 6

slide-16
SLIDE 16

Tuples

  • Tuples fall between strings and lists

§ write them with just commas: 42, 4.0, ‘x’ § often enclosed in parentheses: (42, 4.0, ‘x’)

strings:

immutable sequences of characters

lists:

mutable sequences of any objects tuples: immutable sequences of any objects

Conventionally use lists for:

  • long sequences
  • homogeneous sequences
  • variable length sequences

Conventionally use tuples for:

  • short sequences
  • heterogeneous sequences
  • fixed length sequences

“tuple” generalizes “pair,” “triple,” “quadruple,” …

16

slide-17
SLIDE 17

Returning multiple values

17

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

slide-18
SLIDE 18

Dictionaries (Type dict)

Description

  • List of key-value pairs

§ Keys are unique § Values need not be

  • Example: net-ids

§ net-ids are unique (a key) § names need not be (values) § js1 is John Smith (class ’13) § js2 is John Smith (class ’16)

Python Syntax

  • Create with format:

{k1:v1, k2:v2, …}

  • Keys must be immutable

§ ints, floats, bools, strings § Not lists or custom objects

  • Values can be anything
  • Example:

d = {'ec1':'Ezra Cornell', 'ec2':'Ezra Cornell', 'ela63':'Erik Andersen'}

18

slide-19
SLIDE 19

Using Dictionaries (Type dict)

  • Access elements like a list

§ d['ec1'] evaluates to 'Ezra' § But cannot slice ranges!

d = {'ec1':'Ezra','ec2':'Ezra', 'ela63':'Erik'}

19

'ela63' id8 'Ezra' 'Ezra' 'Erik' dict 'ec2' 'ec1' id8 d

slide-20
SLIDE 20

Using Dictionaries (Type dict)

  • Dictionaries are mutable

§ Can reassign values § d['ec1'] = 'Ellis'

d = {'ec1':'Ezra','ec2':'Ezra', 'ela63':'Erik'}

20

'ela63' id8 'Ezra' 'Ezra' 'Erik' dict 'ec2' 'ec1' id8 d

slide-21
SLIDE 21

Using Dictionaries (Type dict)

  • Dictionaries are mutable

§ Can reassign values § d['ec1'] = 'Ellis'

21

id8 dict id8 d 'ela63' 'Ezra' 'Ellis' 'Ezra' 'Erik' 'ec2' 'ec1' û

d = {'ec1':'Ezra','ec2':'Ezra', 'ela63':'Erik'}

slide-22
SLIDE 22

id8 dict

Using Dictionaries (Type dict)

  • Dictionaries are mutable

§ Can reassign values § d['ec1'] = 'Ellis' § Can add new keys § d['aa1'] = 'Allen'

22

id8 d 'ela63' 'Ezra' 'Ellis' 'Ezra' 'Erik' 'ec2' 'ec1' û

d = {'ec1':'Ezra','ec2':'Ezra', 'ela63':'Erik'}

slide-23
SLIDE 23

id8 dict

Using Dictionaries (Type dict)

  • Dictionaries are mutable

§ Can reassign values § d['ec1'] = 'Ellis' § Can add new keys § d['aa1'] = 'Allen'

23

'aa1' 'Allen' id8 d 'ela63' 'Ezra' 'Ellis' 'Ezra' 'Erik' 'ec2' 'ec1' û

d = {'ec1':'Ezra','ec2':'Ezra', 'ela63':'Erik','aa1':'Allen'}

slide-24
SLIDE 24

id8 dict

Using Dictionaries (Type dict)

  • Dictionaries are mutable

§ Can reassign values § d['ec1'] = 'Ellis' § Can add new keys § d['aa1'] = 'Allen' § Can delete keys § del d['ela63']

24

'aa1' 'Allen' id8 d 'ela63' 'Ezra' 'Ellis' 'Ezra' 'Erik' 'ec2' 'ec1' û

d = {'ec1':'Ezra','ec2':'Ezra', 'ela63':'Erik','aa1':'Allen'}

slide-25
SLIDE 25

id8 dict

Using Dictionaries (Type dict)

  • Dictionaries are mutable

§ Can reassign values § d['ec1'] = 'Ellis' § Can add new keys § d['aa1'] = 'Allen' § Can delete keys § del d['ela63']

25

'aa1' 'Allen' id8 d 'ela63' 'Ezra' 'Ellis' 'Ezra' 'Erik' 'ec2' 'ec1' û

û û

Deleting key deletes both

d = {'ec1':'Ezra','ec2':'Ezra', 'aa1':'Allen'}