пожалуйста, возвращайтесь позднее
пожалуйста, возвращайтесь позднее
>> PARLANTE: So, I just want to make a couple sort of concluding remarks and say, tell you some stuff about Python. I want to show you like, one more Python feature I've been kind of holding back on and then we'll let you go to — go back coding or go have some more free food. So, the first thing I'm going to do — oh, I just have to establish the point of order. All right, so, what does readable code mean? Let's think about that. It's like, well, I would say it's code that if someone who didn't write it, they kind of look down at the printout and they think for like a — they can kind of see what it does. And I'm willing to pause; we'd agree that readable code, that's a good thing. All right, we like code that you can read. All right, so I'm just going to table that, I'm going to put that on the side for use later into this discussion. All right, so I want to show you this feature. This is a feature I kind of thought about showing you. But there was just so much going on. This was just one feature too many for our discussion. But this is something that you're now ready to use. So, we've done a lot of stuff with lists. So, I'm going to set up a list here. I'll have like, you know, "aaaa", "bb" and "ccccc", that's it. All right, there's a list of strings. And suppose I wanted to have a list of their lengths. We've seen how — I could've said, "Well, you know, result, the sequel to the empty list." Then I could've — you know, I looped through list A and added stuff to the result list like. And you know what? That is a fine technique. But Python has a feature called a list comprehension that is a syntax for just making lists with a very kind of compact syntax and just making them in one step. And so, I want to show you the list comprehension syntax. And so my — the problem — my first problem will be like, oh, what if I want to make a list? Link three of the lengths of those strings, so, kind of parallels the list of strings. So, the list comprehension syntax is like — I'm not sure what adjective describes it and so on. But anyway, here's the way I do it. So the list comprehension syntax, what you start off is you write a set of square brackets and then you're going to put code inside of there to represent the list that you want to compute. This is the way I do it, so I type the square brackets and I go into the middle here. And then it — the list comprehension syntax, it reuses as much of the existing syntax as possible. So, the ones — the existing syntax we know is the "for" loop. So, I say — oops. I say, "for s in a." all right, for each string inside of there and then, for the list comprehension syntax. Then on the left hand side, you put the value that it would like to use. What function would you like it to run on each s in this case? So, I'm going to say, "Oh, yeah, what's len(s) for s in a?" And if I just hit return there, that just — in one step, just makes the new list. So, if you wanted to have — you have a kind of a list of one sort, this is a — in a list, you would call this a mapcar. If you have a list of one sort and you want to kind of transform it to a list of another sort, then this is exactly what the list of comprehension does. 00:02:53,539 — > 00:02:57,0 Now, let me show you another example. I'll show you another feature it has. So, if I'll — let's 00:02:57,0 — > 00:03:04,0 say I have a list with, you know, one, two, three, four here. Oops. All right, and now I'll write a comprehension. I'll say, "for num in a."