Lecture 10: Lists and Sequences (Sections 10.0-10.2, 10.4-10.6, - - PowerPoint PPT Presentation

lecture 10 lists and sequences
SMART_READER_LITE
LIVE PREVIEW

Lecture 10: Lists and Sequences (Sections 10.0-10.2, 10.4-10.6, - - PowerPoint PPT Presentation

http://www.cs.cornell.edu/courses/cs1110/2018sp Lecture 10: Lists and Sequences (Sections 10.0-10.2, 10.4-10.6, 10.8-10.13) CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Gries, L. Lee, S. Marschner, C. Van Loan, W.


slide-1
SLIDE 1

Lecture 10: Lists and Sequences

(Sections 10.0-10.2, 10.4-10.6, 10.8-10.13) 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

Lecture 10 Announcements

  • Last call for a one-on-one!

§ CMS: OPTIONAL: one-on-ones

  • Prelim 1 is March 13. LAST DAY to register a

conflict or a need for accommodation. See website:

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

  • CMS: Prelim 1 conflicts
  • A1 Revisions: open from Mar 1st thru Mar 7,

11:59pm. 1 just means “not done”. many people got a 1 due to test cases.

2

slide-3
SLIDE 3

Lecture 10 Announcements A2 is out and about!

Handout provides links to worked examples of diagramming call frames, objects, variables, etc. Preparatory to starting A2: (a) immediately try those worked examples (b) go to office/consulting hours if you have any difficulty This will make A2 go much smoother!

3

slide-4
SLIDE 4

Sequences: Lists of Values

String

s =

  • 'abc d'

Put characters in quotes

  • Use

§ \' for quote character

Access characters with []

  • s[0]

§ is 'a' s[5] § causes an error s[0:2] § is 'ab' (excludes c) s[2:] § is 'c d'

List

  • x = [5, 6, 5, 9, 15, 23]
  • Put values inside [ ]

§ Separate by commas

  • Access values with []

§ x[0] is 5 § x[6] causes an error § x[0:2] is [5, 6] (excludes 2nd 5) § x[3:] is [9, 15, 23]

4

a b c d 1 2 3 4 5 6 5 9 15 1 2 3 4 23 5

Sequence is a name we give to both

slide-5
SLIDE 5

Lists Have Methods Similar to String

  • <list>.index(<value>)

§ Return position of the value § ERROR if value is not there § x.index(9) evaluates to 3

  • <list>.count(<value>)

§ Returns number of times value appears in list § x.count(5) evaluates to 2 x = [5, 6, 5, 9, 15, 23]

But to get the length of a list you use a function, not a class method:

len(x) x.len()

5

slide-6
SLIDE 6

A Word about Testing

Suppose you hear a rumor that the count method is not implemented correctly. Looks good to me! Are we done?

6

>>> lab_scores = [5, 6, 5, 9, 0, 10, 8, 0,7] >>> lab_scores.count(0) 2

slide-7
SLIDE 7

What should I be testing?

Common Cases: typical usage (see previous slide) Edge Cases: live at the boundaries Target location in list:

  • first, middle, last elements

Input size:

  • 0,1,2, many (length of lists, strings, etc.)

Input Orders:

  • max(big, small), max(small, big)…

Element values:

  • negative/positive, zero, odd/even

Element types:

  • int, float, str, etc.

Expected results:

  • negative, 0, 1, 2, many

Not all categories/cases apply to all functions. Use your judgement!

7

slide-8
SLIDE 8

Things that Work for All Sequences

x = [5, 6, 9, 6, 15, 5] s = ‘slithy’

x.index(5) → 0 x.count(6) → 2 len(x) → 6 x[4] → 15 x[1:3] → [6, 9] x[3:] → [6, 15, 5] x[–2] → 15 x + [1, 2] → [5, 6, 9, 6, 15, 5, 1, 2]

x * 2 → [5, 6, 9, 6, 15, 5, 5, 6, 9, 6, 15, 5]

15 in x → True s.index(‘s’) → 0 s.count(‘t’) → 1 len(s) → 6 s[4] → “h” s[1:3] → “li” s[3:] → “thy” s[–2] → “h”

s + ‘ toves’ → “slithy toves”

s * 2 → “slithyslithy” ‘t’ in s → True

methods built-in fns slicing

  • perators

8

slide-9
SLIDE 9

id1 1 2 3 5 7 4

  • 2

list

Representing Lists

Wrong:

9

Correct:

x = [5, 7, 4,-2]

id1 x 5, 6, 7, -2 x Indices

Heap Space Global Space Global Space

slide-10
SLIDE 10

Lists vs. Class Objects

List

  • Attributes are indexed

§ Example: x[2]

Objects

Attributes are named

  • Example:

§ p.x

10

id2 x id3

x 1 y 2 z 3

Point3 id2 1 2 3 5 7 4

  • 2

list id3 p

Heap Space Global Space Heap Space Global Space

slide-11
SLIDE 11

Lists Can Hold Any Type

11

id1 1 2 3 5 7 4

  • 2

list

Heap Space

id2 list_of_strings id2 1 2 3 ‘h’ ‘i’ ‘’ ‘there!’ list

list_of_integers = [5,7,4,-2]

list_of_strings = [‘h’, ‘i’, ‘’, ‘there!’]

id1 list_of_integers

Global Space

slide-12
SLIDE 12

No Really, Lists Can Hold Any Type!

12

id1 list_of_points id1 1 2 3 id2 id3 id6 id7 list

Heap Space Global Space

x 81 y 2 z 3 id2

Point3

x 6 y 2 z 3 id3

Point3

x 4 y 4 z 3 id6

Point3

x 1 y 2 z 2 id7

Point3 id9 list_of_various_types id9 1 2 3 5

3.1416

‘happy’ id5 list

x 10 y 20 z 13 id5

Point3

list_of_points = [Point3(81,2,3), Point3(6,2,3)…]

slide-13
SLIDE 13

Lists of Objects

  • List elements are variables

§ Can store base types and ids § Cannot store folders

13

id1 p1 Heap Space Global Space

x 1 y 2 z 3 id1

Point3

id2 p2 id3 p3

x 4 y 5 z 6 id2

Point3

x 7 y 8 z 9 id3

Point3

p1 = Point3(1, 2, 3) p2 = Point3(4, 5, 6) p3 = Point3(7, 8, 9) x = [p1,p2,p3]

id4

1 2

id1 id2 id3

list

id4 x

How do I get this y?

x[1].y

slide-14
SLIDE 14

id1 1 2 3 5 7 4

  • 2

list

List Assignment

Format

  • :

<var>[<index>] = <value> Reassign at index § Affects folder contents § Variable is unchanged §

Strings cannot do this

  • Strings are

§ immutable

x = [5, 7,4,-2] x[1] = 8 s = “Hello!” s[0] = ‘J’

TypeError: 'str' object does

not support item assignment id1 x

x 8

Heap Space Global Space

“Hello!” s

slide-15
SLIDE 15

What do you think this does?

List Methods Can Alter the List

  • <list>.append(<value>)

§ Adds a new value to the end of list § x.append(-1) changes the list to [5, 6, 5, 9, -1]

  • <list>.insert(<index>,<value>)

§ Puts value into list at index; shifts rest of list right § x.insert(2,-1) changes the list to [5, 6, -1, 5, 9]

  • <list>.sort()

x = [5, 6, 5, 9]

See Python API for more

15

slide-16
SLIDE 16

1st Clicker Question

  • Execute the following:

>>> x = [5, 6, 5, 9, 10] >>> x[3] = -1 >>> x.insert(1, 2)

  • What is x[4]?

A: 10 B: 9 C: -1 D: ERROR E: I don’t know

16

slide-17
SLIDE 17

1st Clicker Answer

  • Execute the following:

>>> x = [5, 6, 5, 9, 10] >>> x[3] = -1 >>> x.insert(1, 2)

  • What is x[4]?

A: 10 B: 9 C: -1 D: ERROR E: I don’t know

17

CORRECT

id1 1 2 3 5 6 5 9

list

4 10 2 id1 x

Heap Space Global Space

x -1

(Original elements 1-4 are shifted down to be elements 2-5)

slide-18
SLIDE 18

Recall: identifier assignment à no swap

18

import shapes def swap(p, q): tmp = p p = q q = tmp p = shapes.Point3(1,2,3) q = shapes.Point3(3,4,5) swap(p, q)

At the end of swap: parameters p and q are swapped global p and q are unchanged

id6 p

Heap Space Global Space

x 1 y 2 z 3 id6

Point3 id7 q

x 3 y 4 z 5 id7

Point3

swap p id7 q id6 tmp id6

RETURN NONE

Call Frame

slide-19
SLIDE 19

Recall: Attribute Assignment à swap!

19

import shapes def swap(p, q): tmp = p.x p.x = q.x q.x = tmp p = shapes.Point3(1,2,3) q = shapes.Point3(3,4,5) swap(p, q)

At the end of swap: parameters p and q are unchanged global p and q are unchanged, attributes x are swapped

id6 p

Heap Space Global Space

x 3 y 2 z 3 id6

Point3 id7 q

x 1 y 4 z 5 id7

Point3

swap p id6 q id7 tmp 1

RETURN NONE

Call Frame

slide-20
SLIDE 20

2nd Clicker Question

def swap(b, h, k): """Procedure swaps b[h] and b[k] in b Precondition: b is a mutable list, h and k are valid positions in the list""” temp= b[h] b[h]= b[k] b[k]= temp x = [5,4,7,6,5]

swap(x, 3, 4) print x[3] id4 x id4

20

1 2 3

1 2 3 5 4 7 6 4 5

What gets printed?

A: 5 B: 6 C: Something else D: I don’t know Heap Space Global Space

slide-21
SLIDE 21

2nd Clicker Answer

def swap(b, h, k): """Procedure swaps b[h] and b[k] in b Precondition: b is a mutable list, h and k are valid positions in the list""” temp= b[h] b[h]= b[k] b[k]= temp x = [5,4,7,6,5]

swap(x, 3, 4) print x[3] id4 x id4

21

1 2 3

1 2 3 5 4 7 6 4 5

What gets printed?

A: 5 B: 6 C: Something else D: I don’t know Heap Space Global Space CORRECT Swaps b[h] and b[k], because parameter b contains name of list.

slide-22
SLIDE 22

2nd Clicker Explanation (1)

def swap(b, h, k): """Procedure swaps b[h] and b[k] in b Precondition: b is a mutable list, h and k are valid positions in the list""” temp= b[h] b[h]= b[k] b[k]= temp x = [5,4,7,6,5]

swap(x, 3, 4) print x[3]

22

1 2 3 swap b id4 h 3 k 4 1

id4 x id4 1 2 3 5 4 7 6 4 5

Heap Space Global Space Call Frame

slide-23
SLIDE 23

2nd Clicker Explanation (2)

def swap(b, h, k): """Procedure swaps b[h] and b[k] in b Precondition: b is a mutable list, h and k are valid positions in the list""” temp= b[h] b[h]= b[k] b[k]= temp x = [5,4,7,6,5]

swap(x, 3, 4) print x[3]

23

1 2 3 swap b id4 h 3 k 4 2

id4 x id4 1 2 3 5 4 7 6 4 5

Heap Space Global Space Call Frame

temp 6

slide-24
SLIDE 24

2nd Clicker Explanation (3)

def swap(b, h, k): """Procedure swaps b[h] and b[k] in b Precondition: b is a mutable list, h and k are valid positions in the list""” temp= b[h] b[h]= b[k] b[k]= temp x = [5,4,7,6,5]

swap(x, 3, 4) print x[3]

24

1 2 3 swap b id4 h 3 k 4 3

id4 x id4 1 2 3 5 4 7 6 4 5

Heap Space Global Space Call Frame

temp 6

5

slide-25
SLIDE 25

2nd Clicker Explanation (4)

def swap(b, h, k): """Procedure swaps b[h] and b[k] in b Precondition: b is a mutable list, h and k are valid positions in the list""” temp= b[h] b[h]= b[k] b[k]= temp x = [5,4,7,6,5]

swap(x, 3, 4) print x[3]

25

1 2 3 swap b id4 h 3 k 4

id4 x id4 1 2 3 5 4 7 6 4 5

Heap Space Global Space Call Frame

temp 6 RETURN

5

None 6

slide-26
SLIDE 26

List Slices Make Copies

26

x = [5, 6, 5, 9] y = x[1:3]

id5 x id5 1 2 3 5 6 5 9 list id6 y id6 1 6 5 list

copy means new folder

Heap Space Global Space

slide-27
SLIDE 27

3rd Clicker Question

27

Execute the following:

  • >>> x = [5, 6, 5, 9, 10]

>>> y = x[1:] >>> y[0] = 7

What is x[1]?

  • A: 7

B: 5 C: 6 D: ERROR E: I don’t know

slide-28
SLIDE 28

3rd Clicker Answer

28

  • Execute the following:

>>> x = [5, 6, 5, 9, 10] >>> y = x[1:] >>> y[0] = 7

  • What is x[1]?

A: 7 B: 5 C: 6 D: ERROR E: I don’t know

CORRECT

id5 x id5 1 2 3 5 6 5 9 list 4 10 id6 y

Heap Space Global Space

id6 1 2 3 6 5 9 list 10 7

slide-29
SLIDE 29

4th Clicker Question

29

A: 7 B: 5 C: 6 D: ERROR E: I don’t know Execute the following:

  • >>> x = [5, 6, 5, 9, 10]

>>> y = x >>> y[1] = 7

What is

  • x[1]?
slide-30
SLIDE 30

4th Clicker Answer

30

A: 7 B: 5 C: 6 D: ERROR E: I don’t know

  • Execute the following:

>>> x = [5, 6, 5, 9, 10] >>> y = x >>> y[1] = 7

  • What is x[1]?

CORRECT

id5 x id5 1 2 3 5 6 5 9 list 4 10 id5 y

Heap Space Global Space

7

slide-31
SLIDE 31

Lists and Expressions / 5th Clicker Q

  • List brackets [] can

contain expressions

  • This is a list expression

§ Python must evaluate it § Evaluates each expression § Puts the value in the list

  • Example:

>>> a = [1+2,3+4,5+6] >>> a [3, 7, 11]

Execute the following:

  • >>> a = 5

>>> b = 7 >>> x = [a, b, a+b]

What is x[2]?

  • 31

A: 'a+b' B: 12 C: 57 D: ERROR E: I don’t know

slide-32
SLIDE 32

Lists and Expressions / 5th Clicker A

  • Execute the following:

>>> a = 5 >>> b = 7 >>> x = [a, b, a+b]

  • What is x[2]?

32

A: 'a+b' B: 12 C: 57 D: ERROR E: I don’t know

CORRECT

5 a id5 1 2 5 7 12 list 7 b

Heap Space Global Space

id5 x

slide-33
SLIDE 33

Lists and Strings Go Hand in Hand

>>> text = 'A sentence is just\n a list of words' >>> words = text.split() >>> words ['A', 'sentence', 'is', 'just', 'a', 'list', 'of', 'words'] >>> lines = text.split('\n') >>> lines ['A sentence is just', ' a list of words'] >>> hyphenated = '-'.join(words) >>> hyphenated 'A-sentence-is-just-a-list-of-words' >>> hyphenated2 = '- '.join(lines[0].split()+lines[1].split()) >>> hyphenated2 'A-sentence-is-just-a-list-of-words'

text.split(<sep>): return a list of words in text (separated by <sep>, or whitespace by default) <sep>.join(words): concatenate the items in the list of strings words, separated by <sep>. Turns string into a list of words Turns string into a list of lines Combines elements with hyphens Merges 2 lists, combines elements with hyphens