Lists
- [Instructor] We've looked at strings in some detail already. In Python, strings and lists are very similar types. The slicing syntax used for strings is also applicable to lists. So if we have a list, one, two, three, four, five, myList, three to the end, there we go. So this is the slicing syntax. Another cool feature of the slicing syntax is that you can use a third value in here. So normally you'd use two values, the start and then the end, but you can also pass in the third value. So if we do myList, zero, six, let's just go from the start to the end with a step size of two, that'll print out every other item. Of course, we can put in a step size of three, that just gives us those two items. And of course, all of this is equivalent to colon colon two, because if you're going from the very beginning to the very end of the list you actually don't have to give any values. It's assumed that you want the beginning and the end. And it might be easier to play around with this feature if we can somehow dynamically generate much longer lists than just typing this all out by hand. And for this, we can use the range function. The range function is a new sequence type we haven't covered yet. It's a bit like a tuple. It has an order and is immutable and isn't frequently used except for looping through it in code like this, for i in range 100, whoops. Print i. Okay, and this is kind of an annoying output so I'm just going to go ahead and clear that cell. But you can also take the range and convert it to a list. So myList equals list, range 100. So we're converting or casting it to a list. And now if we take this and go through every other number, there we go. We can also do a step size of five, all right, or 10. What happens if we enter a negative number? We actually step through the list backwards. So a negative step is stepping backwards through the list. We can even do -10. That's pretty nifty. So all of the things we've been doing so far have been about reading data from lists, taking slices, extracting one value at a time. Let's look at how to actually modify the lists. So let's grab this. Let's actually just do one, two, three, four, myList.append five, and then print myList. Great, append adds an item onto the end of the list. If you want to insert an item at any position you can use insert, myList.insert, position three, so that's the index, a new value, and then print myList. There you go. There are a couple ways to remove items from a list. The first one is called, conveniently enough, remove. myList.remove. And you don't have to pass an index in here, just the value. And then if we look at myList again, you can see that's removed. But be careful, if the item isn't in the list and you try to remove it, it'll throw an error. So if we run this a second time we can see that we get the error. The second way to remove an item from the list is with pop, myList.pop. You don't have to pass an argument at all. It just pops an item off the end of the list and it also returns it to you. So now if we look at myList, there we go. We just have four items in it. We could also do something like this, while length of myList, so while there are items in myList, remember this will evaluate to false when the length of myList is zero, print myList.pop. And if we look at myList now, it's empty. Remember that example earlier about how computers write values in memory? Let's try this out. A is one, two, three, four, five. B is a. a.append six and let's print b. We can see that modifying a also modifies b which we've set a to. But we can use a Python trick here called the copy function. And this makes an identical copy of the list stored separately in memory. So let's take this whole thing. Instead of setting b directly to a, we're going to set b equal to a.copy. And let's just print out both of them so you can see them side by side. And so you can see the difference between setting a variable to another variable and making a copy of it, setting it to the value of that variable. Lists are one of the most fundamental and useful data structures in Python and it's important to get a feel for how they work. So I encourage you to play around with them in this notebook and get ready for some more fun with them as we progress.
随堂练习,边学边练
下载课堂讲义。学练结合,紧跟进度,轻松巩固知识。