字典是从键对象到值对象的映射。 Dictionaries are mappings from key objects to value objects. 字典由键:值对组成,其中键必须是不可变的,值可以是任何值。 Dictionaries consists of Key:Value pairs, where the keys must be immutable and the values can be anything. 词典本身是可变的,因此这意味着一旦创建词典,就可以动态修改其内容。 Dictionaries themselves are mutable so this means once you create your dictionary, you can modify its contents on the fly. 字典可用于对无序数据执行非常快速的查找。 Dictionaries can be used for performing very fast look-ups on unordered data. 关于词典,需要注意的一个关键方面是它们不是序列,因此不保持任何类型的左右顺序。 A key aspect to be aware about regarding dictionaries is that they are not sequences, and therefore do not maintain any type of left-right order. 这意味着,如果在字典上循环,Key:Value对将以任意顺序迭代。 This means that if you’re looping over a dictionary,the Key:Value pairs will be iterated over in arbitrary order. 让我们看一个图表来阐明这个观点。 Let’s look at a diagram to clarify this idea. 我们将建立一个简单的字典,其中有与value对象关联的第一个键。 We’re going to set up a simple dictionary where we have our first key that’s associated with a value object. 我们有第二把钥匙,和另一个物体在一起。 We have our second key that goes with another object. 假设我们这里有第四个键,它和相应的值对象一起。 And let’s say we have key number four here which goes with the corresponding value object. 如果这是一个字典,那么这个键对象将始终与这个值对象相关联。 If this is a dictionary, this key object will always be associated with this value object. 类似地,此键将始终与此值对象一起使用。 Similarly, this key will always go with this value object. 当我们说字典不维护任何类型的左或右顺序时,我们所说的是这些键值对本身的顺序没有定义。 When we say that dictionaries don’t maintain any type of left or right order, what we’re saying is that the ordering of these key-value pairs themselves is not defined. 这意味着如果我在这些对上循环,我可能首先得到对应于我的第二个密钥的对。 That means if I’m looping over these pairs,I might first get the pair that corresponds to my second key here. 然后让我们看看字典的一些用法。 Let’s then look at some uses of dictionaries. 我想编一本叫做《年龄》的字典。 I would like to set up a dictionary which is called age. 如果我希望这是一个空字典,我有两种方法来构造它。 And if I want this to be an empty dictionary,I have two ways to construct that. 第一种方法是只使用一对花括号,这会给我一个字典年龄,在构建时它是空的。 The first approach is just to use a pair of curly braces,and this would give me a dictionary age which would be empty at the time of construction. 另一种可能是键入“age=dict”,这是一个关键字,用左括号和右括号括起来。 Another possibility is to type "age = dict",which is a key word, open and close parentheses. 我又一次创建了一本空字典。 And again I have created an empty dictionary. 然后,让我们构建一个包含姓名和年龄的词典。 Let’s then construct a dictionary that consists of names and ages. 在这个特定的字典中,名称将成为键,年龄将成为值对象。 In this particular dictionary, the names are going to be the keys and the ages are going to be the value objects. 想象一下,我们要做的第一项任务是查找一个人的年龄。 Imagine the first task we’d like to do is look up a person’s age. 我们使用以下语法来实现这一点。 We do this using the following syntax. 我们首先键入词典的名称。 We first type the name of the dictionary. 然后用方括号表示。 We then follow with the square brackets. 在方括号内,插入我们感兴趣查找的键。 Inside the square brackets, we insert the key we’re interested in looking up. 比如说,在这种情况下,我们想知道Pam多大了? Let’s say, in this case, we’d like to know, how old is Pam? 所以我们在方括号内输入“Pam”。 So we’d enter "Pam" inside the square brackets. Python将结果返回给我们。 And Python returns the result to us. 通常,我们希望修改与特定键关联的值对象。 Often, we’d like to modify the value objects that are associated with specific keys. 假设我们想把Tim的年龄提高一年。 Let’s say we would like to increase the age of Tim by one year. 我们可以这样做。 Here is how we could do this. 我们先拿我们的年龄字典。 We first take our age dictionary. 我们想修改提姆。 We’d like to modify Tim. 假设我们想把他的年龄提高一岁。 And let’s say we want to increase his age by one year. 我们可以使用以下语法。 We could use the following kind of syntax. 我们可以说Tim的年龄等于Tim的年龄加1。 We could say age of Tim is equal to age of Tim plus 1. 这当然有效。 And this certainly works. 然而,将一个对象的值增加一个或其他数字是一种非常常见的操作。 However incrementing the value of an object by one, or by some other number,is a very common operation. 在Python中有一个非常方便的简写符号。 And there’s a very handy shorthand notation for doing that in Python. 让我们以Tim的年龄为例,假设我们想将其增加一年,我们可以做以下事情:我们只需输入“+=1”。 So let’s take the age of Tim,and say we would like to increase that by one year,we can do the following:We can just type "+= 1". 如果我们检查Tim的年龄会发生什么,我们会再次将Tim年龄增加一年。 And what happens if we check the age of Tim,we will have increased Tim age again by one year. 让我们确保我们理解当我们说加号等于时会发生什么。 Let’s make sure we understand what happens when we say plus equals. 当您从左向右读取代码时,加号操作首先发生。 When you’re reading code from left to right,the plus operation happens first. 然后我们有了等号,这意味着赋值。 Then we have the equal sign, which means assignment. 例如,当我们说某物加上等于1时,我们首先获取对象的值,我们向该对象添加一个值,然后将其重新分配回原始对象。 So when we say something plus equals 1, for example, we first take the value of the object, we add one to that object,and then we reassign it back to the original object. 一个常见的错误是有时混淆了这两个操作的顺序。 A common mistake is sometimes to mix up the order of these two operations. 想想如果你说x等于+1会发生什么。 Think about what happens if you say x equals plus 1. 您没有将x的值增加1。 You’re not incrementing the value of x by 1. 相反,你是说x应该被赋值为+1,这和x等于1是一样的。 Instead, you’re saying that x should be assigned the value of plus 1, which is just the same thing as saying x is equal to 1. 如果希望将变量的值递增1,请先递增,然后赋值。 If you wish to increment the value of a variable by 1,you first do it the incrementation and then the assignment. 这就是为什么这两个运算的顺序是加号然后等于。 That’s why the order of these two operations is plus and then equals. 您可以使用dictionary方法键来找出dictionary中的所有键。 You can use the dictionary method keys to find out what are all the keys in the dictionary. 同样,您可以使用values方法找出字典中的所有值。 And similarly, you can use the values method to find out what are all of the values in the dictionary. 当您调用这些方法时,Python将返回一个具有非常特殊类型的对象。 When you call these methods, Python returns to you an object which has a very special type. 返回对象的类型称为“视图对象”。 The type of the returned object is what’s called a "view object". 视图对象所做的正是您所想象的。 View objects do precisely what you might imagine that they do. 它们提供字典中键或值的动态视图。 They provide a dynamic view of the keys or values in the dictionary. 这里的一个关键点是,当您更新或修改字典时,视图也会相应地改变。 A key point here is that as you update or modify your dictionary,the views will also change correspondingly. 现在让我们使用刚才定义的字典。 Let’s now work with the dictionary that we just defined. 我们将首先提取字典中的所有名称。 We’re going to first extract all of the names in the dictionary. 所以我会说“names=age.keys”。 So I’ll do that by saying "names = age.keys". 现在让我们检查一下对象名的类型。 Now let’s just check the type of the object names. Python确认这是一个dictionary keys对象。 And Python confirms that this is a dictionary keys object. 假设我想在我的字典中添加一个新键。 Let’s say I would like to add a new key to my dictionary. 我们再加上一个叫汤姆的人,他在我们的字典里已经50岁了。 Let’s add a person called Tom who is 50 in our dictionary. 所以我们说年龄[“Tom”],然后将值对象50赋给该键。 So we say age["Tom"] and we assign the value object 50 to that key. 如果我们现在问,名称是什么,您将看到view对象也包含Tom。 If we now ask, what are the names, you’ll see that the view object also contains Tom. 我们没有重新定义名称的内容,这就是视图对象的本质。 We didn’t redefine the content of names,and this is the nature of view object. 当我们修改字典时,视图对象的内容将自动更改。 As we modify the dictionary, the content of the view object will change automatically. 让我们看看values方法。 Let’s look at the values method. 因此,如果我们说age.values,Python将返回字典中的所有值。 So if we say age.values, Python returns to us all of the values in the dictionary. 我将称之为年龄,我们可以看看这个物体。 I’m going to call this ages,and we can look at that object. 所以在这个例子中,我们有一个,两个,三个,四个,五个不同的年龄段在字典里。 So in this case, we have one, two, three, four,five different ages contained in the dictionary. 我打算在字典里再加一个人。 I’m going to add one more person to the dictionary. 我们给那个人打电话叫尼克,尼克31岁了。 Let’s call that person Nick,and let’s say Nick is 31 years old. 如果我现在问Python,名称是什么,您将看到名称Nick也包含在names对象中。 If I now ask Python, what are the names, you’ll see that the name Nick is also contained in the names object. 这就是视图对象的本质——它们的内容在您修改字典时动态更新。 This is the nature of views objects–their content is dynamically updated as you modify your dictionary. 最后,让我们看看如何测试字典中的成员资格。 Finally, let’s see how we’ll test for membership in a dictionary. 我们知道我们的字典里有一个关键的汤姆。 We know that we have a key Tom in our dictionary. 如果我想问汤姆是否是我们字典的一员,如果汤姆是我们字典里的一把钥匙,我可以问“汤姆”的年龄? If I want to ask if Tom is a member of our dictionary,if Tom is a key in our dictionary, I can ask, "Tom" in age? 答案是“真的”。 And the answer is going to be "True". 如果我问,“佐菲亚”在我们的字典里吗? If I ask, is "Zofia" in our dictionary? Python会说“False”。 Python is going to say "False". 这就是我们在字典中测试对象成员身份的方法。 This is how we test for object membership in a dictionary.