1 / 88

COSC 1306 COMPUTER SCIENCE AND PROGRAMMING

COSC 1306 COMPUTER SCIENCE AND PROGRAMMING. Jehan-François Pâris jfparis@uh.edu. CHAPTER IV LISTS. Introduction. Lists are the most important compound data structure in Python You can do virtually everything with them Tuples and sets are much less important

mdenton
Télécharger la présentation

COSC 1306 COMPUTER SCIENCE AND PROGRAMMING

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COSC 1306COMPUTER SCIENCE AND PROGRAMMING Jehan-François Pâris jfparis@uh.edu

  2. CHAPTER IVLISTS

  3. Introduction Lists are the most important compound data structure in Python You can do virtually everything with them Tuples and sets are much less important Dictionaries are very useful but less general

  4. The basics Lists are sequences of items separated by commas enclosed in square brackets ("[…]") Examples [ 1, 2, 3,4 ,5] ['Alice', 'Bob', 'Carol', Dean'] ['Alice', 'freshman', [100, 90, 70]]

  5. Motivation Let you manipulate composite data: Student records: ["Baker, Jane", 90, 85, 82, 89] Appointments: ["2:30pm", "COSC 1306", "SEC 203", "MW"] Numerous mathematical constructs Cartesians coordinates, complex numbers, vectors, determinants, matrices, …

  6. Usages Group together similar objects Lists of people, courses, exams Vectors and matrices of algebra and physics Store records Contact list entry ['Bill', 713-555-1234, 'bill.tran@ xyx.com'] Anything else

  7. Operations Access the elements of a list Individual elements Slices Modify its elements Insert elements Anywhere Remove elements

  8. Lists >>> mylist = [11, 12, 13, 14, 'done'] >>> mylist[11, 12, 13, 14, 'done'] >>> print(mylist)[11, 12, 13, 14, 'done'] >>> mylist[0]11 We can access individual elements

  9. List slices >>> mylist[0:1][11] Two observations A list slice is a list mylist[0:1] starts with list[0] but stops before mylist [1]

  10. More list slices >>> mylist[0:2][11, 12] Includes mylist[0]and mylist[1] >>> mylist[0:][11, 12, 13, 14, 'done'] The whole list >>> mylist[1:][12, 13, 14, 'done'] The list minus its first (zero-th) element

  11. More list slices >>> mylist[-1:]['done'] A list slice is a list >>> mylist[-1]'done' Not the same thing!

  12. An example a = [10, 20, 30, 40, 50] 10 30 a designates the whole list 20 40 50 a[1] a[0] a[2] a[3] a[4]

  13. Lists in geometry (I) Consider a point Ain the Cartesian plane It has two coordinates xAandyA yA A xA

  14. Lists in geometry (II) We will represent each point in the Cartesian plane by a lista with two elements such that a[0] represents xA a[1] represents yA Can now speak of the origin as point [0, 0]

  15. Distance betweentwo points The distance between two points A and B is the square root of (Ax– Bx)2 + (Ay– By)2 Pythagora's theorema STEM Students mostly

  16. Why? yA A xA STEM Students mostly B yB xB

  17. Function computing this distance >>> def distance( a, b) : """ Cartesian distance between a and b ""“ import math return math.sqrt((a[0] - b[0])**2 + (a[1] - b[1])**2) STEM Students mostly

  18. Function computingthis distance >>> origin = [0, 0] >>> a = [1, 1] >>> distance(origin, a)1.4142135623730951 >>> STEM Students mostly

  19. Lists in physics (I) Speed, accelerations can be expressed by vectors In Cartesian plane force F has two components Fx and Fy We will represent each force F by a listf with two elements such that f[0] represents Fx f[1] represents Fy STEM Students mostly

  20. Lists in geometry (II) STEM Students mostly F Fy θ Fx

  21. Absolute value ofa force >>> def vecmodule(f) """ Module of a two-dimensional vector ""“ import math return math.sqrt(f[0]**2 + f[1]**2) >>> vecmodule([0,2])2.0 >>> vecmodule([3,4])5.0 STEM Students mostly

  22. Adding two vectors (I) [0, 2] + [0, 3][0, 2, 0, 3] Not what we want + operator concatenates lists STEM Students mostly

  23. Adding two vectors (II) STEM Students mostly y Gy F+G F Fy G Gx Fx x

  24. Adding two vectors (III) >>> def vecadd(f, g) : """ adds two two-dimensional vectors ""“ return [f[0] + g[0], f[1] + g[1]] >>> vecadd([0, 2], [0, 3])[0, 5] A function can return a list STEM Students mostly

  25. Multiplying two vectors Which product? Dot product Returns a scalar Cross product Returns a vector Applies to three-dimensional spaces STEM Students mostly

  26. Dot product def dotproduct (f, g): “”” dot product of two two-dimensional vectors ""“ return f[0]*g[0] + f[1]*g[1] >>> dotproduct([1,0], [2, 2])2 STEM Students mostly

  27. List of lists (I) >>> a = [[1, 2], [3, 1]] >>> a[[1, 2], [3, 1]] >>> a[0][1, 2] >>> a[1][1]1

  28. List of lists (II) Can be used to represent matrices ( a00 a00 a01 a11 ) STEM Students mostly

  29. Sorting lists >>> mylist = [11, 12, 13, 14, 'done'] >>> mylist.sort()Traceback (most recent call last): File "<pyshell#2>", line 1, in <module> mylist.sort()TypeError: unorderable types: str() < int() Cannot compare apples and oranges

  30. Sorting lists of strings >>> namelist = ['Alice', 'Carol', 'Bob'] >>> namelist.sort() >>> print(namelist)['Alice', 'Bob', 'Carol'] namelist.sort() is a methodapplied to the list namelist In-place sort

  31. Sorting lists of numbers >>> newlist = [0, -1, +1, -2, +2] >>> newlist.sort() >>> print(newlist)[-2, -1, 0, 1, 2] In-place sort

  32. Sorting into a new list >>> newlist = [0, -1, +1, -2, +2] >>> sortedlist= sorted(newlist) >>> print(newlist)[0, -1, 1, -2, 2] >>> print(sortedlist)[-2, -1, 0, 1, 2] sorted(…) is a conventional Python function that returns a new list

  33. Modifying the elements of a list >>> mylist = [11, 12, 13, 14, 'done'] >>> mylist[-1] = 'finished' >>> mylist [11, 12, 13, 14, 'finished'] >>> mylist[0:4] = ['XI', 'XII', 'XIII', 'XIV'] >>> mylist['XI', 'XII', 'XIII', 'XIV', 'finished']

  34. Lists of lists (I) >>> listoflists = [[17.5, "1306"], [13, "6360"]] >>> listoflists.sort() >>> print(listoflists)[[13, '6360'], [17.5, '1306']] >>> listoflists[0][13, '6360'] >>> listoflists[1][17.5, '1306']

  35. Lists of lists (II) >>> listoflists[0][0]13 >>> listoflists[1][1] '1306' >>> listoflists[0][1] ='6360 quiz' >>> listoflists[[13, '6360 quiz'], [17.5, '1306']]

  36. Adding elements to a list >>> mylist = [11, 12, 13, 14, 'finished'] >>> mylist.append('Not yet!') >>> mylist[11, 12, 13, 14, 'finished', 'Not yet!']

  37. Adding elements to a list (I) >>> mylist = [11, 12, 13, 14, 'finished'] >>> mylist.append('Not yet!') >>> mylist[11, 12, 13, 14, 'finished', 'Not yet!']

  38. Adding elements to a list (II) >>> listoflists = [[13, '6360 quiz'], [17.5, '1306']] >>> listoflists.append([15.3, 'ABET']) >>> listoflists[[13, '6360 quiz'], [17.5, '1306'], [15.3, 'ABET']] Appending means adding at the end.

  39. Adding elements inside a list (I) >>> mylist = [11, 12, 13, 14, 'finished'] >>> mylist.insert(0, 10) >>> mylist[10, 11, 12, 13, 14, 'finished'] >>> mylist.insert(5, 15) >>> mylist[10, 11, 12, 13, 14, 15, 'finished']

  40. Adding elements inside a list (II) >>> mylist = [11, 12, 13, 14, 'finished'] >>> mylist.insert(0, 10) >>> mylist[10, 11, 12, 13, 14, 'finished'] >>> mylist.insert(5, 15) >>> mylist[10, 11, 12, 13, 14, 15, 'finished']

  41. Adding elements inside a list (III) mylist.insert(index, item) index specifies element before which the new item should be inserted mylist.insert(0, item) inserts the new item before the first element mylist.insert(1, item) inserts the new item before the second element and after the first one

  42. Example (I) a = [10, 20, 30, 40, 50] 10 30 a designates the whole list 20 40 50 a[0] a[1] a[2] a[3] a[4]

  43. Example (II) Where to insert 25 and keep the list sorted? 10 30 20 40 a 50 a[0] a[1] a[2] a[3] a[4]

  44. Example (III) Where to insert 25 and keep the list sorted? 10 30 20 40 a 50 a[0] a[1] a[2] a[3] a[4] 25

  45. Example (IV) We do a.insert(2, 25) after a[1] and before a[2]

  46. Let us check >>> a = [10, 20, 30, 40, 50] >>> a.insert(2,25) >>> a[10, 20, 25, 30, 40, 50]

  47. Example (V) Where to insert 55 and keep the list sorted? 10 30 20 40 a 50 a[0] a[1] a[2] a[3] a[4]

  48. Example (VI) Where to insert 55 and keep the list sorted? 10 30 20 40 a 50 a[0] a[1] a[2] a[3] a[4] 55

  49. Example (VII) We must insert After a[4] Before no other element We act as if a[5] existed a.insert(5, 55) It works! Same as a.append(55)

  50. Let us check >>> a = [10, 20, 30, 40, 50] >>> a.insert(5, 55) >>> a[10, 20, 30, 40, 50, 55]

More Related