1 / 18

Data Collections: Lists

Data Collections: Lists. CSC 161: The Art of Programming Prof. Henry Kautz 11/2/2009. Status. Today: Writing functions that work with lists Current workshop: Merge Sort Tuesday: Assignment 7: Literary Analysis Get new partners in lab 11 days to complete

nusa
Download Presentation

Data Collections: Lists

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. Data Collections: Lists CSC 161: The Art of Programming Prof. Henry Kautz 11/2/2009

  2. Status • Today: Writing functions that work with lists • Current workshop: Merge Sort • Tuesday: Assignment 7: Literary Analysis • Get new partners in lab • 11 days to complete • Wednesday: another kind of data collection: dictionaries

  3. Data Types in Python • Simple data types: • Integers • Floating point numbers • Strings • Objects • Combine data with methods • We used in graphics assignment • Lists • Sequence of data elements • Elements can be of any type (simple, objects, lists) • Called "arrays" in some language • The length and contents can vary dynamically

  4. Creating Lists • Writing lists explicitly: >>> [1, 2, 3] >>> ['Henry', 'Kautz', 708] • Creating a list using variables: >>> FirstName = 'Henry' >>> LastName = 'Kautz' >>> Room = 708 >>> Data = [FirstName, LastName, Room] Data ['Henry', 'Kautz', 708] >>> FirstName = 'Sam' >>> Data ['Henry', 'Kautz', 708] The list does not contain the variables, it contains the values the variables have at that moment!

  5. Creating Lists using Functions & Operations • range(start, end, increment): list of integers >>> range(2,12,2) [2, 4, 6, 8, 10] • Concatenation +: creates a new list >>> X = ['dog', 'cat'] >>> Y = ['mouse', 'dog'] >>> Z = X + Y >>> Z ['dog', 'cat', 'mouse', 'dog'] >>> X ['dog', 'cat']

  6. Accessing Elements of Lists • Indexing >>> Z = ['dog', 'cat', 'mouse', 'dog'] >>> Z[0] 'dog' • Slicing >>> Z[1:3] ['cat', 'mouse']

  7. Changing Contents of Lists • Lists in Python are "mutable": you can change elements inside a list without creating a new one >>> Z = ['dog', 'cat', 'mouse', 'dog'] >>> Z[2] = 'horse' >>> Z ['dog', 'cat', 'horse', 'dog'] • Strings are not mutable! >>> S = 'Henry' >>> S[0] 'H' >>> S[0] = 'B' ERROR!

  8. Lists and Aliasing • Two variables might refer to the same list • This is an example of aliasing >>> Z = ['dog', 'cat', 'mouse', 'dog'] >>> W = Z >>> Z[2] = 'horse' >>> W ['dog', 'cat', 'horse', 'dog'] • Why does this happen? • Because an assignment statement puts a pointer to the list into the variable

  9. Aliasing Example >>> Z = ['dog', 'cat', 'mouse', 'dog'] >>> W = Z >>> Z[2] = 'horse' >>> W ['dog', 'cat', 'horse', 'dog'] Z 'dog' 'cat' 'mouse' 'horse' 'dog' W

  10. Avoiding Aliasing • If you are not modifying a list, don't worry about aliasing • You can use the list( ) function to create a new copy of a list >>> Z = ['dog', 'cat', 'mouse', 'dog'] >>> W = list(Z) >>> Z[2] = 'horse' >>> W ['dog', 'cat', 'mouse', 'dog']

  11. Searching Lists • There is a built-in operation for searching a list for an element • <item> in <list> is True if the item is in the list >>> 'cat' in ['dog', 'cat', 'mouse', 'dog'] True >>> 'horse' in ['dog', 'cat', 'mouse', 'dog'] False

  12. Looping Through a List • Combining for with in lets up loop through a list >>> Z = ['dogs', 'cats', 'mice', 'snails'] >>>for A in Z: >>> print 'I like ' + A I like dogs I like cats I like mice I like snails

  13. Looping Through a Slice • You can loop through part of a list using slicing: >>> Z = ['dogs', 'cats', 'mice', 'snails'] >>> S = 'I like ' + Z[0] >>> for A in Z[1:] >>> S = S + ' and ' + A >>> print S I like dogs and cats and mice and snails

  14. List Methods • Surprise: Lists are actually a special kind of object • There are built-in methods for lists • These methods modify their list • Here are some useful ones (see more on page 343) >>> X = ['dog', 'ant', 'cat'] >>> X.sort() >>> X ['ant', 'cat', 'dog'] >>> X.reverse() >>> X ['dog', 'cat', 'ant']

  15. Writing Functions on Lists • When you write your own function that works on a list, you can choose to use iteration or recursion • Example: adding up a list of numbers iteratively def AddUp(L): R = 0 for N in L: R = R + N return R

  16. Recursive Algorithm • Example: adding up a list of numbers recursively: def AddUp(L): if len(L) == 0: return 0 return L[0] + AddUp(L[1:]) • Why does this work? • The empty list is the base case • The slice L[1:] is everything after the first element in the list

  17. Ways to Recurse Through Lists • Recursing through a list one element at a time (e.g. slicing away the first element) is just like iteration • There are other ways you might want to process a list • For example: split the list in halves • Recursively process the first half • Recursively process the second half • You'll do this (or have done this) in workshop this week

  18. Next Class • Workshop solution • One more data collection: Dictionaries • How to implement word counting • We give you this function for use in your next programming assignment

More Related