Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Collections: Lists CSC 161: The Art of Programming Prof. Henry Kautz 11/2/2009.

Similar presentations


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

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 3

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] 4 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'] 5

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

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! 7

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 8

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

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'] 10

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

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 12

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 13

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'] 14

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 15

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 16

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 17

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 18


Download ppt "Data Collections: Lists CSC 161: The Art of Programming Prof. Henry Kautz 11/2/2009."

Similar presentations


Ads by Google