180 likes | 308 Views
ThinkPython Ch. 10 CS104. Students o’ CS104 ‘n’ Prof. Norman. Creating a list. Q: Write code to create a variable cheeses referring to a list containing these strings: ‘cheddar’, ‘edam’, ‘ limberger ’. A: cheeses = [‘cheddar’, ‘edam’, ‘ limberger ’]
E N D
ThinkPython Ch. 10CS104 Students o’ CS104 ‘n’ Prof. Norman
Creating a list Q: Write code to create a variable cheeses referring to a list containing these strings: ‘cheddar’, ‘edam’, ‘limberger’. A: cheeses = [‘cheddar’, ‘edam’, ‘limberger’] Q: Must a list contain items all be of the same type? A: No. For lo!, you can do this: weird = [1, ‘hi’, None, True]
Nested Lists Q: If we nest a list within another list, is there a way to access an element from the nested list? bigMess = [ 1, 3, 5, [ 7, 9 ], [ 44, 22, 11 ] ] How would we print the last item of the first embedded list? A: print bigMess[3][1]
Adding off the end of a list Q: Is it possible to assign an element to a list where the index is much greater than the index of the last element in the list? For example, numbers = [56, 112, 67] numbers[7] = 234 If you can do this, what would the elements of the list between indices 3 and 6 be? A: You cannot do this. You can use append() to add to the end of a list, but you can’t “skip” indices.
Lists are mutable Q: Can you please go over examples that show how lists are mutable? A: OK… You can make a list of floats 1.0, 1.1, 1.2, 1.3, …, 9.9 this way vals = range(10, 100) # integers 10, 11, 12, … 99 for i in range(len(vals)): vals[i] = vals[i] / 10.0
A list is a mapping Q: I don’t understand this mapping stuff. A: A list is a sequence, which means elements of the list are ordered,indexed from 0 up to n – 1. Indices are integers. Therefore, the list contains a mapping from integers 0..n-1 to elements of the list.
Mutating a list: lost data? Q: In section 10.2, I don’t understand why numbers[1] = 5? Where did that come from? Where does the 123 go?? A: From the book: numbers = [17, 123] numbers[1] = 5 Now, numbers holds: [17, 5]
Differences between lists and strings Q: Apart from strings not being mutable, are there any other distinct differences between strings and lists? At least in terms of how they can be manipulated. A: 1) Strings only hold characters. Lists hold anything. 2) Because strings are immutable there are far fewer things you can do to a string than a list. You can’t alter a string (append to it, delete from it, change elements in it, sort it, etc.)
Traversing a list Q: In section 10.3 I get confused when it shows the for loop and says print 'This never happens' so, that is a syntax error then, to have an empty list in a for loop? A: Remember the for loop pattern: for <loopVar> in <sequence>: <body> So, if the sequence is empty, the body isn’t executed. No syntax error.
range(len(somelist)) Q: I didn't understand the use of range(len(somelist)). A: range() can be used for many things, but it is best for creating a list of indices: for i in range(len(numbers)): numbers[i] = numbers[i] * 2 numbers is a list. len(numbers) is an integer, say, n. range(n) produces a list from 0 to n – 1, which is exactly what we need for indices to traverse the list! Lo!
Index-based vs. element-based traversal • A sequence can be traversed either way. • index-based means using indexing with indices to access elements: • for i in range(len(aList)): print aList[i] • element-based means accessing each element directly: • for elem in aList: print elem • Use element-based when you don’t need to know where the element is in the list. index-based otherwise.
10.7 Summary • reduce: analyze a sequence to produce one value. E.g., sum or average of numbers, or concatenation of all strings, or max of all numbers. • map: make a new list from elements of an existing list. E.g., capitalize all strings, create list of floats from list of ints, create list of userIds from list of ints… • filter: create new list from some elements of a list. E.g., find all prime numbers in a list, find all 7-letter words in a list, etc. • Q from student: Do we have to know this stuff? • A from me: Yes.
Lists and strings Q: For this class, can we assume it is always true that a method performed on a list modifies the original argument and returns None, while that method performed on a string returns a new string that can be used in the future? (The book says that's true for "most list methods", not all.) A: All string methods that modify a string return a new string. All list methods that modify a list return None, except pop() which returns the removed element.
split() Q: What does split() do? E.g., soliloquy = “To be, or not 2 b. That is the ?” words = soliloquy.split() A: words is a list of strings, created by removing whitespace in soliloquy: >>> print words [ ‘To’, ‘be,’, ‘or’, ‘not’, ‘2’, ‘b.’, ‘That’, ‘is’, ‘the’, ‘?’ ] Very useful also for processing data files.
Objects and values Q: Can you go over the difference between objects and values, and the distinctions we need to know between the two? A: An object is really a location in memory, and a value is what is stored in that location. That’s why they say a list is an object that holds a sequence of values. And, why two objects can be different but hold the same values. Soon, we’ll create our own types and make objects that can store multiple values in them.
Aliasing Q: Could you give a better description of what aliasing is? A: Sure! Aliasing is when two identifiers point to the same object (the same location in memory). Python does aliasing automatically when multiple variables are created that refer to the same immutable object. This saves memory. Python does not do aliasing for mutable objects unless you use assignment statement.
Note about equivalent and identical • I don’t care if you understand that stuff.
Is aliasing useful? • Yes, very useful. Suppose you are modeling a cave system with rooms connected by hallways. If you leave something in one room and walk around a different way back to that room, you should still find that item. A room object in memory could keep track of that item. No matter how you get to that room object (via different hallway objects), it will be that location in memory holding that item.