lecture 13 - cornell universityannouncements for this lecture! readings! • today: chapter 11! •...

23
More with Sequences Lecture 13

Upload: others

Post on 30-May-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 13 - Cornell UniversityAnnouncements for This Lecture! Readings! • Today: Chapter 11! • Next Week: Sec. 5.8-5.10!! Assignments! • A2 has been graded!! Pick up in Gates

More with Sequences

Lecture 13

Page 2: Lecture 13 - Cornell UniversityAnnouncements for This Lecture! Readings! • Today: Chapter 11! • Next Week: Sec. 5.8-5.10!! Assignments! • A2 has been graded!! Pick up in Gates

Announcements for This Lecture

Readings •  Today: Chapter 11 •  Next Week: Sec. 5.8-5.10

Assignments •  A2 has been graded

§  Pick up in Gates 216 §  Mean: 33, StdDev: 8 §  Grades explained in Piazza

•  A3 is due on FRIDAY §  Turn in before you leave §  Will post survey today §  Survey due next week

•  A4 posted after the exam

10/7/14 More Sequences 2

•  Prelim, Oct 16th 7:30-9:00 §  Material up to TODAY §  Study guide is posted

•  Will Review on Thursday §  Will cover what is on exam §  Set up practice problems

Page 3: Lecture 13 - Cornell UniversityAnnouncements for This Lecture! Readings! • Today: Chapter 11! • Next Week: Sec. 5.8-5.10!! Assignments! • A2 has been graded!! Pick up in Gates

Processing Lists: builtins

• sum(x) adds up all the elements in the list x §  They must all be numbers!

• min(x) or max(x) find the min/max value in list x §  They use the same ordering as sort()

• range(a,b,c) produces [a,a+c,a+2*c,…,a+c*((b-a)/c)] §  Starts at a, increases by c each time, until b (or less) §  The argument c is optional; c = 1 by default

• list(x) converts x (such as a string) to a list §  Example: list('mimsy') produces ['m', 'i', 'm', 's', 'y']

10/7/14 More Sequences 3

Page 4: Lecture 13 - Cornell UniversityAnnouncements for This Lecture! Readings! • Today: Chapter 11! • Next Week: Sec. 5.8-5.10!! Assignments! • A2 has been graded!! Pick up in Gates

The Map Function

• map(⟨function⟩, ⟨list⟩) §  Function has to have

exactly 1 parameter §  Otherwise, get an error §  Returns a new list

•  Does the same thing as def map(f,x): result = [] # empty list for y in x: result.append(f(y)) return result

10/7/14 More Sequences 4

map(f, x)

[f(x[0]), f(x[1]), …, f(x[n–1])]

calls the function f���once for each item

map(len, ['a', 'bc', 'defg']) returns [1, 2, 4]

Page 5: Lecture 13 - Cornell UniversityAnnouncements for This Lecture! Readings! • Today: Chapter 11! • Next Week: Sec. 5.8-5.10!! Assignments! • A2 has been graded!! Pick up in Gates

Lists of Objects

•  List positions are variables §  Can store base types §  But cannot store folders §  Can store folder identifiers

•  Folders linking to folders §  Top folder for the list §  Other folders for contents

•  Example: >>> r = colormodel.RED >>> b = colormodel.BLUE >>> g = colormodel.GREEN >>> x = [r,b,g]

10/7/14 More Sequences 5

id10

red 255

green 0

blue 0

RGB

id11

red 0

green 255

blue 0

RGB

id12

red 0

green 0

blue 255

RGB

id13 x

id13

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

id10 id11 id12

list

id12 g

id11 b

id10 r

Page 6: Lecture 13 - Cornell UniversityAnnouncements for This Lecture! Readings! • Today: Chapter 11! • Next Week: Sec. 5.8-5.10!! Assignments! • A2 has been graded!! Pick up in Gates

Lists of Objects

•  List positions are variables §  Can store base types §  But cannot store folders §  Can store folder identifiers

•  Folders linking to folders §  Top folder for the list §  Other folders for contents

•  Example: >>> r = colormodel.RED >>> b = colormodel.BLUE >>> g = colormodel.GREEN >>> x = [r,b,g]

10/7/14 More Sequences 6

id10

red 255

green 0

blue 0

RGB

id11

red 0

green 255

blue 0

RGB

id12

red 0

green 0

blue 255

RGB

id13 x

id13

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

id10 id11 id12

list

id12 g

id11 b

id10 r

Page 7: Lecture 13 - Cornell UniversityAnnouncements for This Lecture! Readings! • Today: Chapter 11! • Next Week: Sec. 5.8-5.10!! Assignments! • A2 has been graded!! Pick up in Gates

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

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

10/7/14 More Sequences 7

Page 8: Lecture 13 - Cornell UniversityAnnouncements for This Lecture! Readings! • Today: Chapter 11! • Next Week: Sec. 5.8-5.10!! Assignments! • A2 has been graded!! Pick up in Gates

Two Dimensional Lists

Table of Data Images

10/7/14 More Sequences 8

5 4 7 3

4 8 9 7

5 1 2 3

4 1 2 9

6 7 8 0

0 1 2 3

0

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 10 11 12

0 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

Page 9: Lecture 13 - Cornell UniversityAnnouncements for This Lecture! Readings! • Today: Chapter 11! • Next Week: Sec. 5.8-5.10!! Assignments! • A2 has been graded!! Pick up in Gates

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

•  An odd symmetry

§ Number of rows of d: len(d)

§ Number of cols in row r of d: len(d[r])

10/7/14 More Sequences 9

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

0 1

4

2

3

Page 10: Lecture 13 - Cornell UniversityAnnouncements for This Lecture! Readings! • Today: Chapter 11! • Next Week: Sec. 5.8-5.10!! Assignments! • A2 has been graded!! Pick up in Gates

How Multidimensional Lists are Stored

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

•  b holds name of a one-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/7/14 More Sequences 10

id2

9 6 4

id3

5 7 7

id1

id2 id3

id1 b

9 6 4 5 7 7

Page 11: Lecture 13 - Cornell UniversityAnnouncements for This Lecture! Readings! • Today: Chapter 11! • Next Week: Sec. 5.8-5.10!! Assignments! • A2 has been graded!! Pick up in Gates

Image Data: 2D Lists of Pixels

10/7/14 More Sequences 11

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

0 1 2 3 4 5 6 7 8 9

10 11 12

id1 b

id1

id2 id3

list

id2

id23 id24

list

id23

red 255

green 255

blue 255

RGB

b[0][0] is a white pixel

Page 12: Lecture 13 - Cornell UniversityAnnouncements for This Lecture! Readings! • Today: Chapter 11! • Next Week: Sec. 5.8-5.10!! Assignments! • A2 has been graded!! Pick up in Gates

Ragged Lists: Rows w/ Different Length

10/7/14 More Sequences 12

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

•  Will see applications of this later

id2

17 13 19

id3

28 95

id1 id1 b

id2 id3

0 1 2

1 1 0

0

Page 13: Lecture 13 - Cornell UniversityAnnouncements for This Lecture! Readings! • Today: Chapter 11! • Next Week: Sec. 5.8-5.10!! Assignments! • A2 has been graded!! Pick up in Gates

Slices and Multidimensional Lists

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

10/7/14 More Sequences 13

id2

9 6

id1

id2 id3

id1 b

id4

id3

4 5

id4

7 7

x = b[:2]

id5 x

id5

id2 id3

Page 14: Lecture 13 - Cornell UniversityAnnouncements for This Lecture! Readings! • Today: Chapter 11! • Next Week: Sec. 5.8-5.10!! Assignments! • A2 has been graded!! Pick up in Gates

Slices and Multidimensional Lists

•  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 are the contents of the list (with name) in b?

10/7/14 More Sequences 14

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

Page 15: Lecture 13 - Cornell UniversityAnnouncements for This Lecture! Readings! • Today: Chapter 11! • Next Week: Sec. 5.8-5.10!! Assignments! • A2 has been graded!! Pick up in Gates

Slices and Multidimensional Lists

•  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 are the contents of the list (with name) in b?

10/7/14 More Sequences 15

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

Page 16: Lecture 13 - Cornell UniversityAnnouncements for This Lecture! Readings! • Today: Chapter 11! • Next Week: Sec. 5.8-5.10!! Assignments! • A2 has been graded!! Pick up in Gates

Functions and 2D Lists def transpose(table): """Returns: copy of table with rows and columns swapped

Precondition: table is a (non-ragged) 2d List""" numrows = len(table) numcols = len(table[0]) # All rows have same no. cols result = [] # Result accumulator for m in range(numcols): row = [] # Single row accumulator for n in range(numrows): row.append(table[n][m]) # Build up row result.append(row) # Add result to table return result 10/7/14 More Sequences 16

1 2

3  4

5 6

1  3 5

2  4 6

Page 17: Lecture 13 - Cornell UniversityAnnouncements for This Lecture! Readings! • Today: Chapter 11! • Next Week: Sec. 5.8-5.10!! Assignments! • A2 has been graded!! Pick up in Gates

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)

•  Many other applications

Python Syntax

•  Create with format:���{k1:v1, k2:v2, …}

•  Keys must be non-mutable §  ints, floats, bools, strings §  Not lists or custom objects

•  Values can be anything •  Example:���

d = {'js1':'John Smith',� 'js2':'John Smith',� 'wmw2':'Walker White'}

10/7/14 More Sequences 17

Page 18: Lecture 13 - Cornell UniversityAnnouncements for This Lecture! Readings! • Today: Chapter 11! • Next Week: Sec. 5.8-5.10!! Assignments! • A2 has been graded!! Pick up in Gates

Using Dictionaries (Type dict)

•  Access elts. like a list §  d['js1'] evaluates to 'John' §  But cannot slice ranges!

•  Dictionaries are mutable §  Can reassign values §  d['js1'] = 'Jane' §  Can add new keys §  d['aa1'] = 'Allen' §  Can delete keys §  del d['wmw2'] �

d = {'js1':'John','js2':'John',� 'wmw2':'Walker'}

10/7/14 More Sequences 18

'wmw2'

id8

'John'

'John'

'Walker'

dict

'js2'

'js1'

Key-Value order in ���folder is not important

id8 d

Page 19: Lecture 13 - Cornell UniversityAnnouncements for This Lecture! Readings! • Today: Chapter 11! • Next Week: Sec. 5.8-5.10!! Assignments! • A2 has been graded!! Pick up in Gates

Using Dictionaries (Type dict)

•  Access elts. like a list §  d['js1'] evaluates to 'John' §  But cannot slice ranges!

•  Dictionaries are mutable §  Can reassign values §  d['js1'] = 'Jane' §  Can add new keys §  d['aa1'] = 'Allen' §  Can delete keys §  del d['wmw2'] �

d = {'js1':'John','js2':'John',� 'wmw2':'Walker'}

10/7/14 More Sequences 19

'wmw2'

id8

'John' 'Jane'

'John'

'Walker'

dict

'js2'

'js1'

Key-Value order in ���folder is not important

id8 d

Page 20: Lecture 13 - Cornell UniversityAnnouncements for This Lecture! Readings! • Today: Chapter 11! • Next Week: Sec. 5.8-5.10!! Assignments! • A2 has been graded!! Pick up in Gates

Using Dictionaries (Type dict)

•  Access elts. like a list §  d['js1'] evaluates to 'John' §  But cannot slice ranges!

•  Dictionaries are mutable §  Can reassign values §  d['js1'] = 'Jane' §  Can add new keys §  d['aa1'] = 'Allen' §  Can delete keys §  del d['wmw2'] �

d = {'js1':'John','js2':'John',� 'wmw2':'Walker'}

10/7/14 More Sequences 20

'wmw2'

id8

'Jane'

'John'

'Walker'

dict

'js2'

'js1'

'aa1' 'Allen'

id8 d

Page 21: Lecture 13 - Cornell UniversityAnnouncements for This Lecture! Readings! • Today: Chapter 11! • Next Week: Sec. 5.8-5.10!! Assignments! • A2 has been graded!! Pick up in Gates

Using Dictionaries (Type dict)

•  Access elts. like a list §  d['js1'] evaluates to 'John' §  But cannot slice ranges!

•  Dictionaries are mutable §  Can reassign values §  d['js1'] = 'Jane' §  Can add new keys §  d['aa1'] = 'Allen' §  Can delete keys §  del d['wmw2'] �

d = {'js1':'John','js2':'John',� 'wmw2':'Walker'}

10/7/14 More Sequences 21

'wmw2'

id8

'Jane'

'John'

'Walker'

dict

'js2'

'js1'

'aa1' 'Allen' ✗ ✗

Deleting key deletes both

id8 d

Page 22: Lecture 13 - Cornell UniversityAnnouncements for This Lecture! Readings! • Today: Chapter 11! • Next Week: Sec. 5.8-5.10!! Assignments! • A2 has been graded!! Pick up in Gates

Dictionaries and For-Loops

•  Dictionaries != sequences §  Cannot slice them

•  Different inside for loop §  Loop variable gets the key §  Then use key to get value

•  Has methods to convert dictionary to a sequence §  Seq of keys: d.keys() §  Seq of values: d.values() §  key-value pairs: d.items()

for k in d:� # Loops over keys print k # key� print d[k] # value� # To loop over values only�for v in d.values():� print v # value

10/7/14 More Sequences 22

See grades.py

Page 23: Lecture 13 - Cornell UniversityAnnouncements for This Lecture! Readings! • Today: Chapter 11! • Next Week: Sec. 5.8-5.10!! Assignments! • A2 has been graded!! Pick up in Gates

Dictionaries and Lists

•  The values can be lists §  Works just like 2D lists §  But first index is a key

•  Example: >>> d = {} # Empty dict >>> d['wmw2'] = [9,6] >>> d['aa1'] = [4] >>> d['aa1'].append(5)

•  We will use this in A4!

10/7/14 More Sequences 23

'wmw2'

id9

id10

dict

'aa1' id11

id8 d

id10

9 6

list

id11

4 5

list