CS 1110: Introduction to Computing Using Python Lecture 10 Lists and Sequences [Andersen, Gries, Lee, Marschner, Van Loan, White]
Lecture 10 Announcements • Prelim 1 Date: Tuesday, March 14th, 7:30 pm to 9:00 pm Submit conflicts immediately through CMS • A2: You must scan or take a picture of your work to submit it through CMS Since you have been warned to submit early, do not expect that we will accept work that does not make it onto CMS on time. • Set CMS notifications to receive all emails! 3/2/17 Lists & Sequences 2
Sequences: Lists of Values String List • s = 'abc d' • x = [5, 6, 5, 9, 15, 23] 0 1 2 3 4 0 1 2 3 4 5 a b c d 5 6 5 9 15 23 • Put characters in quotes • Put values inside [ ] Use \' for quote character Separate by commas • Access characters with [] • Access values with [] s[0] is 'a' x[0] is 5 s[5] causes an error x[6] causes an error x[0:2] is [5, 6] (excludes 2 nd 5) s[0:2] is 'ab' (excludes c ) s[2:] is 'c d' x[3:] is [9, 15, 23] 3/2/17 Lists & Sequences 3
Sequences: Lists of Values String List • s = 'abc d' • x = [5, 6, 5, 9, 15, 23] 0 1 2 3 4 0 1 2 3 4 5 a b c d 5 6 5 9 15 23 • Put characters in quotes • Put values inside [ ] Use \' for quote character Separate by commas • Access characters with [] • Access values with [] s[0] is 'a' x[0] is 5 s[5] causes an error x[6] causes an error x[0:2] is [5, 6] (excludes 2 nd 5) s[0:2] is 'ab' (excludes c ) s[2:] is 'c d' x[3:] is [9, 15, 23] 3/2/17 Lists & Sequences 4
Lists Have Methods Similar to String x = [5, 6, 5, 9, 15, 23] But you get length of • <list>.index(<value>) a list with a regular Return position of the value function, not method: len(x) 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 3/2/17 Lists & Sequences 5
Things that Work for All Sequences s = ‘slithy’ x = [5, 6, 9, 6, 15, 5] s.index(‘s’) → 0 x.index(5) → 0 methods s.count(‘t’) → 1 x.count(6) → 2 len(s) → 6 len(x) → 6 built-in fn. s[4] → “h” x[4] → 15 s[1:3] → “li ” x[1:3] → [6, 9] slicing s[3:] → “thy” x[3:] → [6, 15, 5] s[–2] → “h” x[–2] → 15 s + ‘ toves’ x + [1, 2] → “ slithy toves” → [5, 6, 9, 6, 15, 5, 1, 2] operators s * 2 x * 2 → “ slithyslithy” → [5, 6, 9, 6, 15, 5, 5, 6, 9, 6, 15, 5] ‘t’ in s → True 15 in x → True 3/2/17 Lists & Sequences 6
Difference: Lists Can Hold Any Type 0 1 2 3 4 5 a list of integers 5 6 8 9 15 23 0 1 2 3 4 5 6 a list of strings ‘H’ ‘e’ ‘l’ ‘l’ ‘o’ ‘ ’ ‘World’ 0 1 2 3 4 a list of objects of class Point id1 id2 id5 id4 id3 0 1 2 3 4 5 6 7 a list of values of 5 ‘a’ ‘joy’ 24.3 id1 id3 0 id2 various types id1 id2 id3 id4 id5 3/2/17 Lists & Sequences 7 Point Point Point Point Point
Representing Lists Wrong Correct x x 5, 6, 7, -2 id1 Unique tab identifier Variable id1 holds id 0 5 1 7 2 4 3 -2 Indices x = [5, 7, 4,-2] 3/2/17 Lists & Sequences 8
Lists vs. Class Objects List Objects • Attributes are indexed • Attributes are named Example: x[2] Example: p.x p id3 x id2 id3 id2 Point3 list 0 5 x 1.0 1 7 y 2.0 2 4 z 3.0 3 -2 3/2/17 Lists & Sequences 9
List Assignment • x = [5, 7,4,-2] • Format : 0 1 2 3 <var>[<index>] = <value> 5 7 4 -2 Reassign at index Affects folder contents • x[1] = 8 Variable is unchanged id1 0 5 1 7 x id1 2 4 3 -2 3/2/17 Lists & Sequences 10
List Assignment • x = [5, 7,4,-2] • Format : 0 1 2 3 <var>[<index>] = <value> x 5 7 8 4 -2 Reassign at index Affects folder contents • x[1] = 8 Variable is unchanged id1 • Strings cannot do this 0 5 x 8 s = 'Hello World!' 1 7 x id1 s[0] = 'J' ERROR 2 4 3 -2 String are immutable 3/2/17 Lists & Sequences 11
Lists and Expressions • List brackets [] can • Execute the following: contain expressions >>> a = 5 >>> b = 7 • This is a list expression >>> x = [a, b, a+b] Python must evaluate it Evaluates each expression • What is x[2]? Puts the value in the list >>> 12 • Example: >>> a = [1+2,3+4,5+6] >>> a [3, 7, 11] 3/2/17 Lists & Sequences 12
List Methods Can Alter the List x = [5, 6, 5, 9] See Python API for more • <list>.append(<value>) Procedure, not a fruitful method 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>) Procedure, not a fruitful method 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() What do you think this does? 3/2/17 Lists & Sequences 14
Clicker Exercise • 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 3/2/17 Lists & Sequences 15
From Before: Attribute Assignment import geom def swap_x(p, q): p = geom.Point3(1.0,2.0,3.0) t = p.x 1 q = geom.Point3(3.0,4.0,5.0) p.x = q.x 2 swap_x(p, q) q.x = t 3 swaps p.x and q.x import geom def swap(p, q): p = geom.Point3(1.0,2.0,3.0) t = p 1 q = geom.Point3(3.0,4.0,5.0) p = q 2 swap(p, q) q = t 3 DOES NOT swap global p and q 3/2/17 Lists & Sequences 16
Lists and Functions: Swap 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] 1 id4 b[h]= b[k] 2 0 5 b[k]= temp 3 What gets printed? 1 4 A: 5 2 7 x = [5,4,7,6,5] B: 6 3 6 swap(x, 3, 4) C: Something else 4 5 print x[3] D: I don’t know x id4 3/2/17 Lists & Sequences 17
Lists and Functions: Swap 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] 1 id4 b[h]= b[k] 2 0 5 b[k]= temp swap 1 3 1 4 b id4 h 3 2 7 x = [5,4,7,6,5] 3 6 k 4 swap(x, 3, 4) 4 5 print x[3] x id4 3/2/17 Lists & Sequences 18
Lists and Functions: Swap 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] 1 id4 b[h]= b[k] 2 0 5 b[k]= temp swap 2 3 1 4 b id4 h 3 2 7 x = [5,4,7,6,5] 3 6 temp 6 k 4 swap(x, 3, 4) 4 5 print x[3] x id4 3/2/17 Lists & Sequences 19
Lists and Functions: Swap 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] 1 id4 b[h]= b[k] 2 0 5 b[k]= temp swap 3 3 1 4 b id4 h 3 2 7 x = [5,4,7,6,5] 3 5 6 temp 6 k 4 swap(x, 3, 4) 4 5 print x[3] x id4 3/2/17 Lists & Sequences 20
Lists and Functions: Swap 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] 1 id4 b[h]= b[k] 2 0 5 b[k]= temp swap 3 1 4 b id4 h 3 2 7 x = [5,4,7,6,5] 3 5 6 temp 6 k 4 swap(x, 3, 4) 4 6 5 print x[3] x id4 3/2/17 Lists & Sequences 21
Lists and Functions: Swap def swap(b, h, k): Swaps b[h] and b[k], """Procedure swaps b[h] and b[k] in b because parameter b Precondition: b is a mutable list, h contains name of list. and k are valid positions in the list""” temp= b[h] 1 id4 b[h]= b[k] 2 0 5 b[k]= temp 3 What gets printed? 1 4 A: 5 2 7 x = [5,4,7,6,5] B: 6 3 5 6 swap(x, 3, 4) C: Something else 4 6 5 print x[3] D: I don’t know x id4 3/2/17 Lists & Sequences 22
List Slices Make Copies x = [5, 6, 5, 9] y = x[1:3] x id5 y id6 id5 id6 list list 0 6 0 5 1 5 1 6 2 5 3 9 copy = new folder 3/2/17 Lists & Sequences 23
Clicker Exercises • Execute the following: • Execute the following: >>> x = [5, 6, 5, 9, 10] >>> x = [5, 6, 5, 9, 10] >>> y = x[1:] >>> y = x >>> y[0] = 7 >>> y[1] = 7 • What is x[1]? • What is x[1] ? A: 7 A: 7 B: 5 B: 5 C: 6 C: 6 D: ERROR D: ERROR E: I don’t know E: I don’t know 3/2/17 Lists & Sequences 24
Lists of Objects • List positions are variables id13 p1 id10 Can store base types list p2 id11 But cannot store folders 0 id10 Can store folder ids p3 id12 1 id11 • Folders linking to folders 2 x id13 id12 Top folder for the list Other folders for contents id12 id10 id11 • Example: Point3 Point3 Point3 >>> p1 = Point3(1.0, 2.0, 3.0) x 7.0 x 1.0 x 4.0 >>> p2 = Point3(4.0, 5.0, 6.0) y 8.0 y 2.0 >>> p3 = Point3(7.0, 8.0, 9.0) y 5.0 z 9.0 z 3.0 >>> x = [p1,p2,p3] z 6.0 3/2/17 Lists & Sequences 25
Recommend
More recommend