180 likes | 253 Views
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
E N D
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 • Wednesday: another kind of data collection: dictionaries
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
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!
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']
Accessing Elements of Lists • Indexing >>> Z = ['dog', 'cat', 'mouse', 'dog'] >>> Z[0] 'dog' • Slicing >>> Z[1:3] ['cat', 'mouse']
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!
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
Aliasing Example >>> Z = ['dog', 'cat', 'mouse', 'dog'] >>> W = Z >>> Z[2] = 'horse' >>> W ['dog', 'cat', 'horse', 'dog'] Z 'dog' 'cat' 'mouse' 'horse' 'dog' W
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']
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
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
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
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']
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
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
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
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