Python Tutorial: Sets - Set Methods and Operations to Solve Common Problems
18:34

Python Tutorial: Sets - Set Methods and Operations to Solve Common Problems

Corey Schafer 20.06.2018 156 464 просмотров 4 750 лайков

Machine-readable: Markdown · JSON API · Site index

Поделиться Telegram VK Бот
Транскрипт Скачать .md
Анализ с AI
Описание видео
In this Python Programming Tutorial, we will be looking at the Set data structure in-depth and discovering how it can help us solve some common problems. The set can not only solve certain problems more quickly but is also more efficient in many cases. Let's get started... The code from this video can be found at: https://github.com/CoreyMSchafer/code_snippets/tree/master/Python/Python-Sets ✅ Support My Channel Through Patreon: https://www.patreon.com/coreyms ✅ Become a Channel Member: https://www.youtube.com/channel/UCCezIgC97PvUuR4_gbFUs5g/join ✅ One-Time Contribution Through PayPal: https://goo.gl/649HFY ✅ Cryptocurrency Donations: Bitcoin Wallet - 3MPH8oY2EAgbLVy7RBMinwcBntggi7qeG3 Ethereum Wallet - 0x151649418616068fB46C3598083817101d3bCD33 Litecoin Wallet - MPvEBY5fxGkmPQgocfJbxP6EmTo5UUXMot ✅ Corey's Public Amazon Wishlist http://a.co/inIyro1 ✅ Equipment I Use and Books I Recommend: https://www.amazon.com/shop/coreyschafer ▶️ You Can Find Me On: My Website - http://coreyms.com/ My Second Channel - https://www.youtube.com/c/coreymschafer Facebook - https://www.facebook.com/CoreyMSchafer Twitter - https://twitter.com/CoreyMSchafer Instagram - https://www.instagram.com/coreymschafer/ #Python

Оглавление (5 сегментов)

Introduction

hey there how's it going everybody in this quick video we're going to talk about python sets so I recently saw an article on python sets and some different use cases for them and I thought it would be a great topic to do a video on so sets are one of those data types that people often forget about but they're extremely useful for solving certain types of problems it's also a popular data type for solving certain job interview questions efficiently so in this video we'll look at some examples and some good use cases for sets so first of all what is a set so a set is kind of like a list but it removes all of the duplicate values now there are also some extra useful methods that we can use with sets that we can't use with other data types so for example we can use the intersection to get all of the elements that are uh the same in multiple sets or we can use the difference method to get all of the elements that are in one set but not others so anytime you're doing comparisons where you're uh creating lists of values that are in one list and not another list or anything like that then sets are usually a great way to go so first of all let's just look at some simple examples so to create a set we can simply pass in a list of values to the set function so I have a set here that just takes in a list of numbers 1 through five and then I'm printing that out so if I save this and run it then we can see that prints out this set and it looks like a list but it has these curly brackets instead of square brackets so we can create a set that way as well so I could take the same values and just replace the set function with these numbers inside the curly braces so I will just replace where we're creating this set and instead you can just create a set this way like a list but with the curly braces so if I save that and run it then you can see that we get the same result now one thing that you have to remember when doing it this way is that if you want to create an empty set then you can't simply just get rid of all these values and use an empty uh set of the curly braces because this will actually create an empty dictionary um so to create an empty set you actually have to use that set function and just pass in no arguments so that will create an empty set but the empty curly braces will create an empty dictionary so just be on the lookout for that if you want

Adding and removing values

to create an empty set okay so I also said that sets remove duplicate values so if I add in some of the same numbers here to the end of our set so I'll just add in a 1 two and three if I save that and run it then you can see that in our set we still just get 1 through five and it remove those duplicate values okay so now I'm going to remove those duplicates and just get back to where uh we were um okay so just like with a list we can also add and remove values from a set so let's say that we wanted to add some more values to the set so to add a new value we can simply come to the next line and say s do add and we can add a six so if we save that and run it then you can see that the six was added to our set now if this kind of stuff doesn't seem useful yet then we will be seeing some practical examples of using sets in just a minute um okay so now what if you wanted to add multiple values to your set then you could use the update method and pass in a list or another set so if we wanted to add the values of 678 then instead of just doing add which will add one value I could say S1 update and now we'll pass in a list of values that we want to add so now if we run that then we can see that using that update of 67 and 8 added 67 and 8 into our set and we can update with another set as well so if I made another set here called S2 and I'm going to also add some duplicate values to this as well so I'll do uh 789 in that set and then within our update let's update with this list of six 78 and then also add in S2 to that update so it's going to add the values from both this list and from this set so if I save that and run it then we can see that the values from both that list and that set were added to S1 and as usual the duplicate values were removed since this is a set okay so now what if we wanted to remove some values then we can either use the remove or discard methods so let's look at the difference between the two of these so if we wanted to remove the value of five then I'm just going to get rid of these lines here if we wanted to get rid of the value of five from S1 then I could simply say S1 do remove 5 and if I save

Removing values

that and run it then we can see that value was removed and if we use the discard method here then that would give us this exact same result the difference between remove and discard is if we try to remove a value that doesn't currently exist in our set so if I try to remove a value of six here which isn't in our S1 set if I save that and run it then you can see that we get a key error and it says the key error we don't have a value of six and that is whenever we try to remove a value that doesn't exist but using discard won't throw a key error if a value doesn't exist so if I go up here to remove and change this to discard and save that and run it then you can see that it just prints out our set with no values removed um so that's the difference between remove and discard okay so now let's look at some more useful things that we can do with sets so everything we've seen so far is pretty basic and similar to list but now let's look at operations like intersection and difference and Union and things like that so let me grab some different sets from my Snippets here so that we can see some of the useful operations that we can do with these So within my Snippets I'm just going to grab this first part here and paste this into uh my mod module okay so we can see

Setting values

that we have three different sets here of 1 2 3 4 and 345 so they have some overlapping values and they have some unique values so let's say that we wanted to get a set of values that are in all of these sets now if these were lists then you could probably come up with a function or a list comprehension that could do this but with sets this is as simple as running the intersection method so to do this I can come down here and I can say I'll create a new set and call this S4 and our new set will just be S1 do intersection and we will get the intersection for now we'll get the intersection of S2 so this will give me all of the values that are in S2 that are also in S1 so if I uncommon out that print statement there and save that and run it then we can see that we get a set of two and three and the reason we got two and three is because two and three are in both S1 and in S2 so it didn't get the one or the four okay so now what if I add S3 as an argument to our intersection here so this is going to give us all of the values in S2 and S3 that are also in S1 um so before we even run this uh most likely we can tell that this is going to return a three because we can see that there is a 3 and S2 and A3 and S3 and that is the only value in those two sets that is also in S1 up here at the top so if I save that and run it then you can see we got what we expect the value of three so these are operations that we can kind of do in our head with these small sets like this but if these sets were much larger you know thousands of uh values then these operations save us a lot of time and efficiency uh by doing this quickly okay so we got our value that intersects with all three sets uh now what if we want the values that are different so to do that we can use the difference method so if I want to see what values are in S1 that are not in S2 then I could come down here and say S1 dot difference and then pass in S2 as a value um so most likely we are going to get a one for this since one is in S1 and it is not in S2 two and three are within S2 so we shouldn't get that as a result so if I save that and run it then you can see that we do get that result of one now you might be wondering why it didn't also return a four here since four is in S2 but not in S1 but that is because we are running the difference method on S1 if we wanted the values that were in S2 but not in S1 then we could simply flip these so I could say s2. difference and then pass in S1 as my argument if I save that and run it then we get the value of four because four is in S2 but not in S1 now if we wanted uh the values that were different between both of those sets so we wanted to return a set of one and four that is called a symmetric difference and we'll look at that in just a second but for now let's keep looking at this difference method so we can pass in multiple sets as arguments here as well so if I want the values that are in S2 but not in set one or set three then I could say s2. difference and then pass in S1 as an argument and S3 as an argument so if I save that and run it then you can see that we get an empty set and that's because if we look at our examples then S2 doesn't have any values that aren't in either S1 or S3 so we can see the first two values here two and three uh we have a 2 three and S1 and also a three and S3 and for this four we have that four value in S3 as well so there are no values in S2 that aren't in either of those lists now if I was to change this around and say that I want the values uh the s3. difference of S1 and S2 then this is saying give me the values that are in S3 that are not in either S1 or S2 so if we look at this example then it should return a five because that's the only value that is not in either of these other two lists so if I save that and run it then you can see that we get a set of five okay so I said we'd also look at the symmetric difference now the symmetric difference uh allows you to compare two sets and get all of the differences between both sets so if I go back to my original example here of S1 do difference with S2 remember if I ran this then we just got a one but it didn't include this four which is in S2 but not in S1 so if we wanted all of the values that were different between those two sets then I could simply just say symmetric underscore difference make sure I typed that right I think I did so I'll save that and run it and now you can see that we get one and four and in this case it shouldn't matter if we have S2 do symmetric difference of S1 or which order those are in because it's going to give us the differences from both sets alog together so the one here is unique to set one and the four two um okay so now let's look at some more practical examples of where we use some sets or some of these techniques so first of all like we said before this is a great way to remove duplicate values from a list so let's say that we have a list that has some duplicates so I will just create a list here called L1 and I will fill this in with one two uh 3 1 2 3 and save that and we want to list that is equal to

Operations

this one but with the duplicates removed so we might be tempted to write a small function that keeps appending the values to a new list one at a time and skips the values that have already been added but that's a lot more complicated than it needs to be um so first of all we could simply say L2 is equal to a set of L1 and what that will do will cast our list to a set and remove the duplicates but we're still left with a set so now we can simply cast this set to a list again by wrapping that result uh within a list so if we save this and run it whoops and I did not print that out so let me do a print of L2 so if I save that and run it then you can see that we have a list of one two three which is our original list with the duplicates removed so the inner function here uh casts this to a set and removes the duplicates and the outer function here cast it back to a list and we can see that we got the result that we were looking for and the set approach is also much faster than any function that you could write with the same functionality so it's more efficient as well okay so now let me grab some more code from my Snippets here and we'll look at some more examples of the operations that we can do so I'm going to grab these lists here and paste them in to my file here and let me make this just a little smaller so that it all fits on the same line okay so I've got three different lists here one is a list of employees another who have gym memberships and the last list is a list of employees who are developers now this is just an example but you can imagine situations where you'd have lists and subl lists like this that are much larger maybe something that comes from a datab base or something like that so let's imagine that these lists could be much larger and that we want to gather some information from these so first of all let's see which employees have both a gym membership and are also developers so to do this we can simply intersect the gym members with the developers so let's try this out so I'll say result is equal to and we will get a set of the gym members and we will do an intersection so intersection and we will intersect that with the developers and then right underneath here I am going to print our result um now you could also cast developers to a set here also if you'd like but it's okay to pass it in as a list as well so we have to cast the first one to a set though uh because this intersection is a method of a set so we had to cast that so if I save that and run it and I meant to pass in developers not just developer so I'll save that again and run it and you can see that we get a result of April and qu and if we were to go up here and compare these two lists then you would find that these are the two employees who are in both the developers list and the gym members list and you could also cast that back to a list if you'd like if you wanted a list as a result here okay so now what if we wanted to get all of the employees who are neither gym members or developers so to do this we could use the difference method on our employees and then compare that to our other two lists so to do that I could simply say uh so I'll cast employees to a set and then I'll say Uh I that I want the difference and of people who are not gym members or developers so again what we're doing here is that we're getting uh back all of the employees who are neither gym members or developers so that is why we have employees in this first part here and then passing in gym members and developers into this difference method here so if I save that and run it then if we were to go up here and actually compare these lists then we could see that this is the result that we get back of the two employees who are not in the gym member list or in the list of developers so shame on those two for not having an awesome job or staying in shape okay so that is just a couple of examples of how you can solve some of these problems with sets so there are a lot of problems that you'll run into like this so definitely keep sets in mind if you're trying to solve a problem that involves comparing values between lists um now one more thing I think I should mention about sets is that they're very performant when it comes to doing membership tests now if you don't know what I mean by membership tests basically I mean that if you're doing um let's see let me replace this line here if you're doing a lot of stuff like this so if uh Corey in uh developers then you know print um found so this is actually a membership test here where you're looking for this value in our list then it's actually more performant to do those membership tests on sets rather than list so if your lists are huge and you're doing a lot of comparisons like this then it should speed you up a good bit if you're able to convert those lists into sets and check that way um so for those of you who know bigo notation stuff uh it's bigo of n to check if a value is in a list so let me write that down that is Big O of n for a list and it is O of one which is constant for a set and the reason it's Big O of n for a list is because it has to scan the whole list until it finds the value and for sets it's just constant time okay so I think that is going to do it for this video I hope that after this video you have a better idea for how you can use sets to solve different problems in your daily workflow uh but if you do have any questions about what we covered then feel free to ask in the comments section below and I'll do my best to answer those and if you enjoy these tutorials and would like to support them then there are several ways you can do that the easiest ways is simply like the video and give it a thumbs up and also it's a huge help to share these videos with anyone who you think would find them useful and if you have the means you can contribute through patreon and there's a link to that page in the description section below be sure to subscribe for future videos and thank you all for watching

Другие видео автора — Corey Schafer

Ctrl+V

Экстракт Знаний в Telegram

Экстракты и дистилляты из лучших YouTube-каналов — сразу после публикации.

Подписаться

Дайджест Экстрактов

Лучшие методички за неделю — каждый понедельник