Placeholder Image

字幕表 動画を再生する

  • how everyone in this video I'm gonna show you how to use sets in python.

  • Now, what is a set more in python?

  • A set is a type of data.

  • The store's a set of things, and this is actually a set of unique things.

  • So, for example, if you have a set of these two numbers one and three and if you try to add another number three here to this set, nothing happens.

  • Used to have this set one on three because a python set just rejects any duplicate elements.

  • Okay, let's take a look at some code examples here.

  • If you write a eco's sit parentheses, this creates a new empty set, and then it assigns it to this viable A.

  • So if you print a by writing print A and then once you run the cell, this thing is printed, said parentheses.

  • This just were presents an empty set, all right, And to add elements to this set, for example, a number you can just write a dot add parentheses, one on this as the number one to the set.

  • So once you print a, you see this thing curly brackets and they want inside this just represents a sit with only one inside the set on.

  • After that, if you write a dot, add to it'll add to to this set.

  • And then once you're print again, you see that Now this is a set of two numbers one and two.

  • And if you try to add the number two again by writing it up, add to nothing happens on you.

  • Still get the same sit because again, a python said rejects any duplicates.

  • Okay, One useful thing to do with the set is to reiterate over every element in the set.

  • So you can do that with four X in a and then Prince X.

  • So you see that this is exactly the same as how we generate over each element in the list.

  • So if a was a list, you know this part would look exactly the same.

  • On this way, we can generate over every element in the given set eight.

  • So let's see if it works by running this.

  • So on the dust, we see one and two being printed.

  • Okay, so when should you use a set?

  • One example is winning.

  • When I remove duplicates from, let's say, a given list.

  • So let's say you're given this list called Given list one with thes elements 112 for on two here.

  • If you want to remove the duplicates, you know these two ones on thes two twos so that you only have one, too, on four in, let's say, a new list.

  • You can use a set here.

  • To do that, you can just write this first create and use it by writing news it.

  • Let's say one eco's set parents sees this creates an empty set, and then after that, you can go over each almonds in the given lists by writing for X and given list.

  • One colon, four spaces, new sets, one that ad X.

  • So this way we're saying we want to add each of these elements 1124 and two to this.

  • Use it, use it one, And then after that, you just need to print new set one on.

  • What do you think we're gonna get?

  • Will get 12 and four because we're adding every element in the given list.

  • But this set automatically rejects any duplicates, and that's why we're left with these unique elements.

  • Okay, And what if you want to create a new list.

  • The only contains these unique elements from the original list.

  • I think about it for a second on Here's my solution.

  • First, create a new list by writing new lists one Eco's square brackets.

  • This creates a new empty list.

  • You can do the same thing by writing lists parentheses as well, and then go over every element in the set in this set 12 and four by writing for X and use that one colon for spaces newest ce one that upend X So this way, we're going over every element in the new set one.

  • And they were a pending that two.

  • New list one by writing newest one dot upend x.

  • So after that, once your prints new lists.

  • One.

  • You should see this list 12 and four on just a quick note.

  • Here, you can add things to a set that are not numbers.

  • So, for example, you can do this.

  • Be Eco's sit.

  • This creates an empty set on assigns It to Be, and then you can write be that odd single quotes apple and maybe the odd single quotes banana.

  • And this way, we're adding the strings apple and banana to the set.

  • And you can even mix multiple types of things in a single set so you can do add one, and then this as apple banana, which are strings on one, which is a number two the same set beat.

  • So watch your print be You should see this one banana and apple, Okay?

  • And I think one thing to note here is that the you know the order in which you see these elements One banana on Apple is different from the order in which we added these things Apple banana on their one.

  • So a set is a type of data that doesn't store the order in which things have been added to the set.

  • And that's one way in which a set is different from a list in python.

  • And that's because when you add things to a lists by using the upended function like we saw earlier here, the order in which these things have been added to lists is preserved.

  • Okay, let me give you a simple exercise problem to practice using where you've just learned.

  • So let's say you're given this list given list, too.

  • On with these elements.

  • 1341 and three.

  • The problem is, can you find the some off unique elements in this list?

  • So the unique elements are obviously 13 on four on the some of those elements is eight.

  • So your solution should be able to find this number eight.

  • Your solution should work, no matter how many elements are in the given lists.

  • So think about it for a second on.

  • I'm gonna give you my solution in the second.

  • Okay, here's my solution.

  • First, create and use it by writing new on the score set to Eco's set currencies and then run a four loop over Given list to buy rating for X and given list to colon.

  • New set to dot add X.

  • So this way we're adding all the unique elements to this set.

  • So after this four loop, this is gonna have the elements 13 and four.

  • And to find the some off those elements, there's actually a shortcut for Ian Python.

  • You could just write some you said to, but here, let's not use that for now.

  • And let's do it in a more explicit way.

  • So we'll first create a new viable, called a total, and I Let's set it to there and there will just add every element in new set to two total.

  • We can do that by writing for X, and you said to Colin Total plus Eco's X.

  • So this way we're adding each element in new sets to which we're calling X two total.

  • And once your prints total, you should get eight.

  • Let's save it works on it does.

  • Okay, that's it for this video.

  • Thank you, as always for watching my videos on.

  • I'll see you guys in the next one.

how everyone in this video I'm gonna show you how to use sets in python.

字幕と単語

ワンタップで英和辞典検索 単語をクリックすると、意味が表示されます

A2 初級

Pythonでセットを使う方法 (Pythonチュートリアル #13) (How To Use Sets in Python (Python Tutorial #13))

  • 10 0
    林宜悉 に公開 2021 年 01 月 14 日
動画の中の単語