字幕列表 影片播放 列印英文字幕 Hi, everyone in this video, I'm going to cover the basics off list comprehension in Python To explain this concept, let me give you an example here. Suppose you're given this list a eco's scar Buckets 13579 on 11. And for whatever reason, you want to create this new list to 6 10 14 18 on 22. As you can see in this new lists, each elements of this list is double that of the original list. So, for example, the first element is double the first element or the original list. And if you look at the last element 22 that's double the last element off the original lists. Eight. On one way to create this list is to use Thea Pen function for the lists. Object now, just a quick review off the appendage functional. Here, let's say you have be Eco's scar brackets, so a B is an empty list. Then you can a pin a new element to this list just by writing. Beat up a pen prostheses on whatever you want to add to this list. So let's say upend 10 on After that, If you write beat up upend 20 you'll be able to add the number 22 this list as well. So after that, if you print be, you should see 10 on 20 because those are the moments that you just appended to this list on. That's why you see here 10 and 20. So with that in mind, you can complete the or is no task by first writing. See Eco's scar buckets. So by first creating an empty list on After you can run a four loop for each element in a so four, Let's say ex in a and again the list A. Contend these numbers 135 and some. And then for each X in a we're gonna write si dot up in X times. Two. On this way. Once you finish running the four loop on, wash your prince see where you should see is double. Each element's off the oars, analysts. A. So let's see if that's the case on it. ISS to 6 10 and sewn. Now you can do the same thing using a list comprehension with python. For that, you can just write. Let's say the this gonna be the newest eco's scar brackets X times two for X in a on. That's it. The way you can read this is that the is gonna be a lists in which each element of this list is gonna be X times two for each X in a. So this is exactly the same as what we did earlier, but, you know, slightly simpler syntax. And then after that, if he prints the used to see exactly the same thing. So let's run this cell on. That's what we see. 26 10 on zone. Okay, now I'm gonna give you one more example Here. Let's say you want to create this list instead. 149 16 25 on 36 as a guest. See, this is one squared two squared, which is 43 squared. Four squared, five scored on six squared, which is 36. You can also create this list in two different ways. One way, just like before, is to use Thea Pender function. So for that you can first create an empty lists by writing. Let's say you won ecos square brackets and then four x in range off one comma seven and then the one that upend ex star star, too. So ex star star, too. That's how you say X squared in python And then once you're prince, the one you should see this list. So remember that range off Juan. Comma seven gives us the range starting from one up to seven, but not including seven. So this gives us one through six. Once your prints, you won. You see the list that we want it. You can create the same lists with a list comprehension as well. For that, you can write e to Eco's score buckets X that will start to so X squared for X, a range long comma seven And then once you're print you two, you should see the same list here. Ah, you do Now I'm gonna give you one little exercise problem here. Let's say you want to create this list. 36 25 16 941 So the elements contained in this lis are the same as the previous list here, but it's just in the ops, the order. So that's six squared five skirt and sewn down to one squared. Create this list in two different ways. You know, one using the upended function on the other way is gonna be using a list comprehension. Okay, here's my solution. The first thing you need to do is you need to create a new range the ranges from six down 21 For that, you can write range off 60 on, then minus one. This says, create a new range that starts at six and then that ends at zero, but not including those on. Instead of going up by one, we want the range to go down by minus one instead. So that's why I wrote minus one for the third argument here. So if you, for example, print what's inside this range by writing for X in range 60 minus one and then Prince X, where you should see is 65432 on one on. That's exactly what you see here. On using this range, you can create the list that we wanted to create. Let's do that with the A pen functional first. So here I'm gonna write F one Eco's scarf rockets, so create on empty list and then for X in range off 60 minus one colon if one that upend X double store, too. So x squared and then Prince F one. And this gives us 36 25 on zone The list that we want it. You can do the same thing, of course, with a list comprehension. Just by writing f to ECOWAS square brackets X, that will start to four X, Arrange off 10. I mean, 60 and then minus one. Once you print this, I used to see the same lists. Ah, you do right here. Okay, that's it for this video. Let me know in the comments section below, if you have any questions on Sorry for the delay. I stopped creating this Siri's. But, you know, I got a lot of requests for more videos on. That's why I started crediting more videos. Anyway, thank you, as always, for watch my videos on.
B1 中級 用Python理解列表基礎 (Python教程#12) (List Comprehension Basics with Python (Python Tutorial #12)) 9 0 林宜悉 發佈於 2021 年 01 月 14 日 更多分享 分享 收藏 回報 影片單字