课程: Python Essential Training

List comprehensions - Python教程

课程: Python Essential Training

List comprehensions

- [Instructor] Python has a really neat feature that's somewhat unique in programming languages, and that is the list comprehension. Note the word comprehension here has nothing to do with understanding but more to do with the word comprehensive, so systematically listing everything out. For example, my list is one, two, three, four, five. Let's write a list comprehension with this, two times item for item in my list. Isn't that neat? So square brackets surround the list comprehension and then there's a syntax frame much like the for loop that we saw before. So for item in my list. Of course item can be any variable name that you want. Just make sure that you use the same variable name over here when you're doing stuff with it. A list comprehension allows you to essentially make a for loop in one line while returning a copy of the list that you're iterating over. It also allows you to filter or call functions on every item or of a list. So let's play around with it. So like I said, the list comprehension can act as a filter. So for example, my list is equal to list range 100. So this is going to give us a list with all the numbers from zero to 99. Let's make a filtered list, it's equal to item for item in my list if item modulus 10 is equal to zero. Remember, this is the modulus operator. It gives you the remainder after dividing the variable here by the number on the right. So whenever the remainder after dividing item by 10 is equal to zero, this statement is true. So item for item in my list, if it's divisible by 10. And let's see what this looks like. All of the numbers that are divisible by 10. We could even do something like this. Let's change this to less than three. That's pretty neat. It gives you all the numbers that end in zero, one, or two. And let's just make this a print statement to make it a little easier to read. There, lie flat. List comprehensions are really great for working with strings as well. I want to show you a new string function called split. Split splits a string based on a character or another string that you give it. For instance, myString is equal to, my name is Ryan Mitchell, I live in Boston. Okay, myString.split. And this splits it into two sentences based on where the period is. If nothing is passed in, the split function will split on spaces. myString got split and there we go, that splits it into words. We might want to get rid of that period though, and maybe just sort of normalize all the text a little bit. So for this, let's make a new function called cleanWord. And this is going to return word.replace, period with an empty string, and dot lower. So here the replace function replaces any instances of the first string, in this case a period, with the second string, which is empty. Effectively, this removes all the periods. And then the next function makes the string lowercase. Calling one function after the other in a single line like this is called chaining functions. It can be handy sometimes to keep your code clean, but be careful not to go too crazy with it or it can lead to long and unreadable lines of code. So let's use this now in a list comprehension. cleanWord, word for word in myString.split. So now we have this nice clean list of all the words in the text. Let's say that we want to clean and filter at the same time. For instance, only getting the small words in the text. We could do something like this. If length of myString, or sorry, if the length of cleanWord word is less than, let's say three. So that gets all the one and two letter words in the text. So you can see how powerful list comprehensions are. I use them all the time for cleaning text and processing large amounts of data. And as one final example before I officially dub you the list comprehension expert, the nested list comprehension. So let's just give a quick example of that. cleanWord word for word in sentence.split for sentence in myString.split on the period. So don't get too overwhelmed by this. All this is doing is splitting this original text into sentences and then performing sort of an inner list comprehension on each sentence. So now we have a list of lists, sort of like a two-dimensional structure that groups these clean words by the sentence that they appeared in. So have fun with these. Using list comprehensions is a great way to write clean, readable, and as Python programmers say, Pythonic code.

内容