WHT

Python Collections Library | Collections In Python – Advanced Python 06 – Programming Tutorial

Python Engineer

Subscribe Here

Likes

618

Views

25,719

Collections In Python - Advanced Python 06 - Programming Tutorial

Transcript:

Hi, everybody, welcome to a new. Python tutorial today we’re going to talk about the collection’s module in Python. The collections module implements special container datatypes and provides alternatives with some additional functionality compared to the general built in containers like dictionaries, lists or tuples, so we will be talking about five different types from the collections module, the counter, the named tuple, the order sticks, the default dict and the deck, So let’s start with the counter and first of all we have to import it from collections. Import counter and the counter is a container that stores the elements as dictionary keys in their counts as dictionary values. So let’s say we have a string called a with some different characters AAA BBB CCC. And then we can create our counter. We say my counter equals counter, and then we give it our string, and if we print it, then we see we have a dictionary with all the different characters as keys and their count as values, so we have five times a four times B and three times C and like with a normal dictionary, we can have a look at only the items, so this will give us all the key value pairs. We can have a look at the keys, so this will give us an iterable over the keys, and we can also only have a look at the values, so this will give us all the different values and what’s also very helpful is to have a look at the most common element in our counter dictionary, so we say if we first print our counter again and then we can say we want to print my counter dot most common and then here, how many different items so I want to see only the very first, so the the most common, um, element. So if I print this, then I will get the a with the count. 5 is the most common element. So if I say 2 here, then it will give me the two most common types, so it will also put the B in here and this will return a list with tuples in it. So for example, if I want to have a look at only the I want to see. What is the most common element? Then I will except to access the index 0 so this will give us the tuple and index 0 and then if I only want to see the element, then I will again have to access the first element of this tuple so again 0 and then I will get the A is the element that is most common in our string, so we can also use a list here or any other iterable. Yeah, we can also have a list with all all the different elements, so if we say print my counter dot elements and this will give us an iterable over elements repeating each as many times as it counts, so I have to convert this to a list in order to print it nicely. So now if I print it and I will see, I will get all the different elements here as a list and I can, for example, iterate over this, so that’s the counter next talk about the name Stupa. And, of course, first of all we have to import it, so we say from collections import named tuba and the named Tuple’s is an easy to create and lightweight object type similar to a struct. So what I can do is I can define my named Tuple. I say, for example, let’s create a 2d point and call it point equals, and then I will say named Tuple and then as first argument, I give it the class name, so typically, this is the same name that I used here and then as a second argument, I use another string and here I use all the different fields. I want separated by either A comma or a space, so I can say X comma Y. So this will create a class called point with the fields X and Y So now I can create this point, so I can say PT equals point, and then I will give it well used for X and Y So, for example, I will give it 1 and minus 4 and now if I print my point, then I will see. I have a point with X equals 1 and Y equals minus 4 and I can also access the field, so I can say PT DOT X and PT DOT Y So then this will print the values for X and Y next is the ordered dictionary so from collections import ordered DICT and the ordered Stick is just like a regular dictionary, but they remember the order that the items were inserted, so they have become less important. Now since the built in dictionary class has also the ability to remember the order since Python 3.7 this is guaranteed, but for example, if you use an older Python version, this may be a way to use a dictionary that remembers the order, so for example, let’s create a dictionary like so, and then we can append key value pairs like with a normal dictionary, so we say here and brackets give it a key a and a value 1 and let’s do this with some more key values, So let’s say we have B C and D and 2 3 4 and now if you print this, then we see it’s the same order as we inserted it. So, for example, if we inserted the a at the very end, then it will also get printed at the end of our ordered dictionary. Yeah, since here. I’m using 3 Python three-point-seven so in this, I can also just simply use a normal dictionary now, and it still remembers the order next. We have a look at the default stick so from collections import default stick and the default tick is also similar to the usual dictionary container, with the only difference that it will have a default value if the key has not been set yet. So what we will do. We have to create a default stick and as an argument, we will give it a default type, so let’s say we want to have an INT an integer here as default type, and then we can fill our dictionary again. Let’s say D with the key. A is 1 and D with the key B equals 2 and lets. Print our dictionary, so we will see it here, and then we can access the key. So for example, it’s XS the key a and then it will give 1 and the key B will return -. And now if I put in a key that does not exist, so for example, C then what will happen, it will return the default value of an integer and this is by default a zero, so I can also, for example, say I want a float default value, so then this will return 0.0 If it does not exist or for example, I will have an empty list. If it does not exist, so yeah, with a normal dictionary, This would raise a key error so now this would raise a key error. But with the default ticked, it would return the default value of the type that we specify so as a last collections type, we will talk about the deck, so the deck is a double ended queue and it can be used to add or remove elements from both ends and both are implemented in a way that this will be very efficiently. And, yeah, let’s create a deck, so let’s say D equals deck. And then we can append items like with a list. Let’s say D Append 1 and D Append 2 and then print it. Now now we see our deck here, and also we can say we can say. D Dot append left so this will add elements at the left side. So now we can see our 3 got added here and we can also again remove elements from both sides so we can say D Dot pop, and now if we print our deck, then we will see that with pop. This will return and remove the last element so now the two got removed or we can say D Dot pop left, so this will return and remove the element from the left side so now the 3 got removed, you can also, of course, a D dot clear, so this will remove all elements. We can extend our deck with multiple elements at a time so we can see D Dot extend and then give it a list. Let’s say 4 5 6 so this will add all the elements at the right side or we can say D Dot extend left, so this will extend all the elements at the left side and note that now it will at first the 4 from the left side and the 5 and then the 6 so now 6 is the most left element in our deck. We can also rotate our text so we can say D Dot rotate 1 and now if we print it, we will see that this will rotate all elements. One place to the right. I can also say, for example, T dot rotate 2 and then this will rotate all elements. Two places to the right, or if I want to rotate to the left side and I will give a negative number here. So if I say D Dot rotate minus 1 then all our elements will rotate one place to the left, and yeah, that’s all. I wanted to show you about collections. I hope you enjoyed this tutorial and see you in the next tutorial where we will talk about the itertool’s module in Python.

0.3.0 | Wor Build 0.3.0 Installation Guide

Transcript: [MUSIC] Okay, so in this video? I want to take a look at the new windows on Raspberry Pi build 0.3.0 and this is the latest version. It's just been released today and this version you have to build by yourself. You have to get your own whim, and then you...

read more