Dictionaries
(gentle music) - In my experience, most data structures you work with in Python will be lists, but that's followed very closely by dictionaries. Lists and dictionaries, dictionaries and lists. They work together like, well, like duct tape and WD-40. You know, if it's moving and it shouldn't, use duct tape, if it should move and it doesn't, use WD-40. It's a complete solution for every situation. So with lists and dictionaries combined, you can solve just about any problem with Python. That'll be easily 95% of the data structures you're working with. Let's go to the code. So let's start working with this dictionary of animals I've created. Here, I've left a comma at the end of this last key value pair in the dictionary, and this is called a trailing comma. So you don't technically need this. This code will run just fine without it, but it is good practice. It's convention to have it on the end there. So, if we do animals a, we get aardvark back. We can add a dog by using a similar syntax. Animals d is equal to dog. Great. And if we print out the dictionary, of course, we can see that's added there. Now let's update the dictionary, change a to antelope, and print out the dictionary. Great, we've made a an antelope instead. If we want to get the keys and values of the dictionary, animals.keys and animals.values, and that just prints out the keys and values. And notice that this is a dict keys object rather than a list. It looks kind of like a list. You can iterate over dict keys. They are immutable. You can't change them directly, you just add to them. You have to modify the underlying dictionary in order to change them, but you can convert them to list. So if I take this, pass in that, there we go. We've actually just made a copy of this, of this keys dict keys object and turned it into a list. Okay, so what if I try to access a key that's not present? Animals e. We get an error. So, there is a function that's pretty handy for this and that's called the get function, animals.get e, and as a second argument, you can pass it a default. All right, so it didn't find e, it returned elephant instead. You don't have to have a default value there. This will actually, if we print this out, you can see it is a none object that gets returned if you don't provide a default value. And of course, if we do something like this, we'll get antelope back because that does exist in the dictionary. Like strings and lists, you can use the length function on the dictionary. So length animals, we get four because there are four keys in our dictionary. Let's look at one really common pattern in programming, and that's a dictionary where the values are lists. So I'm just going to copy this, and let's just keep the first two and then turn this into a list. Sorry, a dictionary of lists. And then we can add our old friend the antelope here. Okay, great. So, if we want to add bison to the structure of animals, that's pretty straightforward. Animals, bison starts with b, and then we just append bison. Okay, great. But what if we want to add cat? The list for c doesn't exist, so we have to do animals c is equal to, rather than just depending, is equal to a new list containing cat. But what if there is already a caribou in that list and we just overwrote it? What if we don't know whether that key exists or not? We just know what we want to add. So, we have to write something like this. If c not in animals, animals c is equal to a new list. And then, in either case, c append cat. Great, so that works. And notice that the not in operator tells you if that key exists in the dictionary or not. Is there a solution to this sort of convoluted code? And there is, that is the, as you can probably guess from the title down here, the default dict. So we have to import this from the collections package, from collections import default dict. Okay. So now we have access to it. To create a new default dict, you need to pass the type of object that it should return by default. So animals equals default dict list. We want to return a list. So a common mistake here, a lot of people will return or pass in, rather, an instance of the thing that they want returned. But you actually have to give it a sort of callable function, say, use this every time you need to make a new thing that gets returned. So now we have our new default dict, and if we look at it, it doesn't have any values in it yet. So let's make one, animals e append elephant, and let's take a look at it now. Great. We didn't have to say, hey, this is equal to a new list. It just did all the work for us and automatically added that. If we say animals e append emu, that gets added as well to our new list. And the really cool thing is if we say animals f, we get back an empty list because we haven't added anything there yet. While lists and dictionaries work excellently when combined with each other, the same cannot be said for WD-40 and duct tape. So in that way, I think you'll find Python data structures are far superior.
随堂练习,边学边练
下载课堂讲义。学练结合,紧跟进度,轻松巩固知识。