Python Lists, Dictionaries, Sets & JSON (1.3)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi this is Jeff Veen welcome to applications of deep neural networks with Washington University in this video we're going to look deeper into the Python programming language and see how to use lists and dictionaries this allows complex data structures to be created in the Python programming language and it very much mirrors JSON you can literally emulate the JSON syntax right in Python code much like you can do in JavaScript to build these complex structures for the latest on my a I course and projects click subscribe in the Bell next to it to be notified of every new video in this part we're going to take a look at Python lists dictionary sets and JSON so if you've already worked with these topics you can safely skip this section and continue onward like most programming languages Python does have a notion of arrays lists dictionaries and sets what's neat about the way the Python does this which is pretty similar to the way that the JavaScript does this is the code that makes up these lists is often valid JSON inside a Python if you write it correctly it'll always be valid JSON so what we'll see here is in Python we can have a list of values this is an array this is a predefined array we have strings in here single quote single quote and we can print these out when you print out a list in Python it usually looks like this you'll have a open brace close brace and then whatever is inside of it there some lists if you're using numpy lists what will should we get into later that will look a little more complex down there but it'll still be basically the same thing you have a number of values in there in a lot of programming languages arrays are of a fixed size in Python they're not you can add to them so C dot append E is going to put something else into the list after the list was created so that's that's kind of handy you can iterate over a list this is a for each like they have in many programming languages so this is going to loop over every value that is an S and print them out so there you have the the values that were in that collection or list another thing that python has that is that it's kind of handy is it will let you keep track of what value you're at in the list if you wanted to keep track of the index you'd have to do something like this index equals zero print you'd print out the s that you have and then maybe the index that it's at and then you'd have to remember to increment that each time and now you could keep track of where you were in the list otherwise you have no notion of where you're at in the list each of these iterations of the list is is exactly the same in terms of knowing actually where you're at so we'll leave that like it is there but here we can use something called an enumeration that keeps you from having to have that other value so now we're looping over I comma C I is going to be your index and C is going to be the value at that particular index so now you you know where you're at in this this would be useful because maybe you would want to modify the collection at that particular value and change it to something else we're not going to do that but that is where this might be useful or if just you wanted to print numbers to go along along with these now everything in python is zero based lists and indexes start at zero you can also define your list by adding values to it by appending to them and we add these in this this value this is not a typo there are meant to be two C's because that shows you that a list which always has the square brackets can have more than one value more than one item that has exactly the same value if you use something called a set and this is very useful you can use a set to eliminate duplicates as you add these in that second C doesn't get added because it says hey I've already got one of those to define a set you just do C equals set and then close on parenthesis now you'll notice it does have the curly braces when it prints out so it's somewhat like a dictionary now lists can have values added and removed so here we have ABC we insert a 0 now notice we're inserting it at location 0 so it goes at the beginning and we print it we see that it popped into the beginning then we're going to remove from C the value of B so the collection is C we can also remove add an index so if you want to remove 0 the first one that's the way that you do that so this is how you can very dynamically add and remove values to arrays as you go it's not like some programming languages where you define a fixed length for the array and that's it now this is kind of the neat part of this you can define dictionaries and hash tables and create fairly complicated structures so here I am creating a this is basically name value pairs and this is essentially dictionaries and hash tables by themselves dictionaries Maps hash tables those names all mean very similar things and are essentially interchangeable for the most part Here I am creating a dictionary on a dictionary a book you look up a word and you find a definition this is pretty much what it does here so name is jeff' address is 123 main if I print out D so when I run this first thing you're gonna see is just the dictionary printed out if I print out this is how you make it look it up if I print out name and square braces it'll find Jeff and print out Jeff this is how you check to see if something is in the dictionary or not now if you try to access something it's not in the dictionary like if I tried to print out name too it's going to give an error so you want to check for that so if name is indeed name is defined which it is age is not defined so be aware of that as you use dictionaries this is a very common feature of Python that we will definitely use throughout this course you can also access the individual keys and values in this so if you run this one it's going to say the keys are name and address and I notice it says that this is a dictionary cheese this is basically a list you can treat it like a list or you can convert it into a list easy enough just by passing it to a list values these are the values so you use the keys function and the values function to gain access to the entirety of what is in a dictionary you can also combine them so here this is very common you'll see a list and then each of these maps in here or dictionaries is essentially a record so it's seen in the first record the person's name or the names my wife and I Jeff and Tracy Heaton we have three pets two birds and a dog named hickory and the paths since we have multiple pets you have to put a list in there if it was just went in' we could have just did : and win so long as you define the format to be in that way John Smith here has one pet cold Rover since we defined the format to be a list we expect a list and even though he it just has Rover presumably a dog we don't need to really have the list but it makes it convenient because we can then expect everything to be a list consistency is always a good thing John Doe has no pets so I don't know what his problem is this is then the complete list of customers we can print this out or we can iterate over it with for each so this prints out the whole thing just dumps the whole thing to your to your screen or here we can loop through them and maybe you could handle each one you could count the number of pets that each person had and by the way this is kind of handy too customer get so if you do get instead of just the the braces then you can provide a default so the default here is no pets so if there were no pets for this person which is the case for for Jindo it'll simply say no pets and by the way this is where this code starts to look very much like JSON between here how you end up with code not being JSON is JSON requires quotes here if you were to change this to that now you're no longer valid JSON more advanced lists this is kind of neat you can zip two lists together so here we have 1 2 3 4 5 5 4 3 2 1 for B and we're going to print out the zip of a and B it's going to connect those two together now that just gives you an object to actually see it you do this these are tuples tuples and lists are pretty similar in Python we really won't get into the differences of what those are for the study of neural networks but you can see that the first one is here 1 2 3 4 5 and then 5 4 3 2 1 is the other one so this is now created a series of tuples together in the list so you have a list that contains the tuples and then the tuples are the the union of those two those two lists or the connection of those two lists and you can also use it like this so now you are using the x and y that are coming out of each of those we already saw the enumeration but that's basically just so you can track which one is at each value so you know that one is at location 0 and so on this can be useful to do things like that where you want to now what each index holds this is a comprehension in Python will use those some basically what this is doing is it builds up a list on the fly for you so this is saying for all the X's in 10 and whatever you put here if you just put X here it would just duplicate the list but since it's 10 this creates a second list where every value is multiplied by 10 this is a very handy way to build up lists on the fly you can also build up a dictionary on the fly I use this a lot when I'm dealing with CSV files so when you deal with CSV files you'll get a list of their headers so you might have column 0 column 1 column 2 and column three you might want a lookup table so that you could look up the text of Col to now this could be something like address or whatever you could pass that string in and then if it ever changes position it'll it'll it'll move to the right index you create this lookup value and the look of dictionary says the column 0 is 0 column 1 is 1 column 2 is 2 now if you add something into here like I don't know just that then now it's going to track those so column 3 got shoved over so it's still 4 this is how you make your code not break when you have changes made to your code so this is very handy because now you can look up the index of that column and find out that it is in fact - thank you for watching this video in the next video we're going to look at how to make use of files both image and text as you import data for your deep neural networks this content change is often so subscribe to the channel to stay up to date on this course and other topics and artificial intelligence
Info
Channel: Jeff Heaton
Views: 17,442
Rating: undefined out of 5
Keywords: python, python introduction, python introduction deep learning, deep learning, neural network, deep neural network, jeff heaton, Keras, TensorFlow, Washington university, wustl, json, sets, dictionary, data structure
Id: kcGx2I5akSs
Channel Id: undefined
Length: 12min 57sec (777 seconds)
Published: Wed May 22 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.