Placeholder Image

字幕列表 影片播放

  • When coding you spend a lot of time making lists in many languages, this can be tedious, created, empty list said it before loop, then add the items to the list.

  • One by one.

  • Python cares about your sanity and gives you a tool to simplify this process.

  • List comprehension.

  • In most cases, list comprehension is let you construct a list in a single line of code.

  • It's now time for Python to shine and save time with a single line.

  • We will cover many examples of list comprehension sze.

  • But first, let's talk about them.

  • Generally in python lists are a collection of data surrounded by brackets, and the elements are separated by Commons.

  • A list comprehension is also surrounded by brackets.

  • But instead of a list of data inside, you enter an expression followed by four loops.

  • And if classes here is the most basic form for a list comprehension, the first expression generates the elements in the list, and you follow this with a four loop over some collection of data.

  • This will evaluate the expression for every item in the collection.

  • If you only want to include the expression for certain pieces of data, you can add on an if Klaus after the four loop, the expression will be added to the list only if the if Klaus is true.

  • You can even have more than one if Klaus and the expression isn't the list on Lee.

  • If all the if clauses air true and you can even look over more than one collection, let's now see some examples.

  • For our first example, let's create a list of the squares of the 1st 100 positive integers.

  • Let's first do this without list comprehension.

  • Sze To begin, you might create an empty list called squares.

  • Next, you would loop over the 1st 100 positive integers you would then a pen the square off I to the list squares.

  • Don't forget that an exponents in Python is represented by double asterisks.

  • Why, oh, why do they not use the intergalactic mathematical notation for exponents to see that this works?

  • Print the list Squares.

  • Let's do this once more.

  • Using list comprehension.

  • Sze.

  • We will call the list comprehension squares too open with a bracket.

  • Then first type the expression for each term in the list.

  • Finally, enter the desired four loop.

  • If you print this, you get the exact same list, but we only needed one line of code instead of three.

  • Let's now look a slightly more complex example.

  • We'll create a list of remainders when you divide the 1st 100 squares by five to find the remainder.

  • When you divide by five, use the percent operator in mathematics.

  • This is the same as taking the square mod five.

  • If you print the list, you'll see there are only three perfect squares.

  • Mod 501 and four.

  • Let's do this again, but look at the remainder is when you divide the squares By 11 we see the perfect squares.

  • Mod 11 are 013459 This example shows you that the expressions in the list comprehension can be complex.

  • By the way, if you look at the remainder is when you divide by a prime number P, you'll notice an interesting pattern.

  • The number of remainder XYZ P plus one divided by two.

  • The problem of finding which numbers appear in the list is a complex puzzle from number theory known as quadratic reciprocity and was first proved by ghosts.

  • Next, let's create a list comprehension that hasn't if Klaus suppose we have a list of movies and we want to find those movies that start with the letter G.

  • Let's see how to do this with and without list comprehension.

  • Sze.

  • If you are not using list comprehension Sze, you'd start by making an empty list.

  • Next loop over the list of movies we can use The starts with method to see if the title starts with the letter G.

  • If it does, then appended to our list, print the list to make sure that it worked.

  • But this four line routine can be done in a single line with a list comprehension.

  • The expression we want to appear on our list is simply the title next loop over the movies.

  • But also check that the title starts with the letter G print and observe.

  • We get the same answer with a single line of code.

  • Let's complicate this example a bit more.

  • Suppose our list of movies is a list of two bowls containing both the title of the movie on the year it was released.

  • What if we want a list of the titles of all movies that were released before the year 2000?

  • How would you do this using list comprehension?

  • Sze.

  • As before, we want our this to only contain the titles.

  • But this time, when we write the four loop, each element is the trouble.

  • Next we select the movie's released before 2000 using an If Klaus on the year.

  • If you print the list, you can see that it worked in this example.

  • The if block used the year, but the year was not included in the list.

  • On Lee, the title is included.

  • Let's see a mathematical example.

  • Suppose you use a list to represent a vector.

  • How would you perform scaler Multiplication on this vector?

  • That is what if we want to multiply each number by four, you might be tempted to try four times V, But look what happens.

  • This is unusual.

  • What happened here is four times V is the same as V plus V plus P plus V and in python.

  • If you add two lists, it can cap needs them rather than adding them.

  • Component Waas, for example, if you add 246 and 13 you get the list.

  • 24613 so four times V is just a list containing four copies of E.

  • This is not what we want.

  • We can achieve scaler multiplication with a list comprehension.

  • Just make a list comprehension where you multiply each component by four.

  • If you print this factor, you can see we get the desired result.

  • For our final example, let's useless comprehension is to compute the Cartesian product of sense.

  • The Cartesian product is named after the French scholar money.

  • They got recall that if you have two cents A and B, then the Cartesian product is the set of pears, where the first component isn't a on the second component is from B.

  • You can write this mathematically like this.

  • For example, if A is the set 13 and B is the set X Y, then the Cartesian product is the set of pairs one x one y three x and three wives.

  • Let's use less comprehension is to compute the Cartesian product of two cents.

  • Let a be the list of odd integers 135 and seven.

  • Let Bebe the list of even integers 246 and eight to compute the Cartesian product, create a list comprehension where each term is the Triple A B next write a four loop over the elements of a and then a four loop over the elements of B.

  • If you print the product, you can see the list contains all 16 possible pairs.

  • Using this technique, you can even compute the Cartesian product of three or more sense with list comprehension.

  • Sze You can build complex lists using a single line of code on with Socratic A.

  • You can learn complex topics in a single video, so please upend yourself to our list of subscribers.

  • And if you are in a position to help financially, we would be honored to add you to our list of patrons.

  • Now go forth and code compactly, do you?

When coding you spend a lot of time making lists in many languages, this can be tedious, created, empty list said it before loop, then add the items to the list.

字幕與單字

單字即點即查 點擊單字可以查詢單字解釋

B1 中級

列表理解 || Python教程 || 學習Python編程 (List Comprehension || Python Tutorial || Learn Python Programming)

  • 5 0
    林宜悉 發佈於 2021 年 01 月 14 日
影片單字