Python Data Structure 简明教程
Python - Dictionary
在字典中,每个键都用冒号 (:) 与其值分隔,项目用逗号分隔,整体用花括号括起来。没有项目的空字典只用一对花括号编写,如下所示 − {}。
In Dictionary each key is separated from its value by a colon (:), the items are separated by commas, and the whole thing is enclosed in curly braces. An empty dictionary without any items is written with just two curly braces, like this − {}.
键在字典中是唯一的,而值可能不是。字典的值可以是任何类型,但键必须是不可变数据类型,例如字符串、数字或元组。
Keys are unique within a dictionary while values may not be. The values of a dictionary can be of any type, but the keys must be of an immutable data type such as strings, numbers, or tuples.
Accessing Values in Dictionary
要访问词典元素,你可以使用熟悉的方括号和键来获取其值。
To access dictionary elements, you can use the familiar square brackets along with the key to obtain its value.
Example
一个简单的例子如下 −
A simple example is as follows −
#!/usr/bin/python
dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}
print ("dict['Name']: ", dict['Name'])
print ("dict['Age']: ", dict['Age'])
Output
执行上述代码后,将生成以下结果 −
When the above code is executed, it produces the following result −
dict['Name']: Zara
dict['Age']: 7
如果我们尝试使用一个键访问一个数据项,该键不是字典的一部分,我们便会得到如下错误 −
If we attempt to access a data item with a key, which is not part of the dictionary, we get an error as follows −
Updating Dictionary
你可以通过添加新条目或键值对、修改现有条目或删除现有条目来更新词典,如下面的简单示例所示 −
You can update a dictionary by adding a new entry or a key-value pair, modifying an existing entry, or deleting an existing entry as shown below in the simple example −
Delete Dictionary Elements
你可以移除单个字典元素或清除整个字典的内容。你也可以在单个操作中删除整个字典。
You can either remove individual dictionary elements or clear the entire contents of a dictionary. You can also delete entire dictionary in a single operation.
Example
要显式移除整个字典,只需使用 del 语句。一个简单的例子如下 −
To explicitly remove an entire dictionary, just use the del statement. A simple example is as mentioned below −
#!/usr/bin/python
dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}
del dict['Name']; # remove entry with key 'Name'
dict.clear(); # remove all entries in dict
del dict ; # delete entire dictionary
print ("dict['Age']: ", dict['Age'])
print ("dict['School']: ", dict['School'])
-
Note −that an exception is raised because after del dict dictionary does not exist any more −
Properties of Dictionary Keys
词典值没有限制。它们可以是任何任意的 Python 对象,无论是标准对象还是用户定义的对象。但是,键并非如此。
Dictionary values have no restrictions. They can be any arbitrary Python object, either standard objects or user-defined objects. However, same is not true for the keys.
关于字典键,有两个重要的点需要记住 −
There are two important points to remember about dictionary keys −
-
More than one entry per key not allowed. Which means no duplicate key is allowed. When duplicate keys encountered during assignment, the last assignment wins.
For example
#!/usr/bin/python
dict = {'Name': 'Zara', 'Age': 7, 'Name': 'Manni'}
print ("dict['Name']: ", dict['Name'])
Output
执行上述代码后,将生成以下结果 −
When the above code is executed, it produces the following result −
dict['Name']: Manni
键必须是不可变的。这意味着你可以使用字符串、数字或元组作为字典的键,但诸如 ['key'] 的类型不被允许。
Keys must be immutable. Which means you can use strings, numbers or tuples as dictionary keys but something like ['key'] is not allowed.