Python 简明教程

Python - Dictionary View Objects

dict 类的 items()keys()values() 方法返回视图对象。每当其源 dictionary 对象的内容发生任何更改时,这些视图都会动态刷新。

The items(), keys(), and values() methods of dict class return view objects. These views are refreshed dynamically whenever any change occurs in the contents of their source dictionary object.

The items() Method

items() 方法返回一个 dict_items 视图对象。它包含 listtuples ,每个元组由各自的键值对组成。

The items() method returns a dict_items view object. It contains a list of tuples, each tuple made up of respective key, value pairs.

Syntax

以下是 items() 方法的语法 −

Following is the syntax of the items() method −

Obj = dict.items()

Return value

items() 方法返回 dict_items 对象,它是 (key,value) 元组的动态视图。

The items() method returns dict_items object which is a dynamic view of (key,value) tuples.

Example

在下面的示例中,我们首先使用 items() 方法获取 dict_items 对象,并在更新字典对象时检查它的动态更新方式。

In the following example, we first obtain the dict_items object with items() method and check how it is dynamically updated when the dictionary object is updated.

numbers = {10:"Ten", 20:"Twenty", 30:"Thirty",40:"Forty"}
obj = numbers.items()
print ('type of obj: ', type(obj))
print (obj)
print ("update numbers dictionary")
numbers.update({50:"Fifty"})
print ("View automatically updated")
print (obj)

它将生成如下输出:

It will produce the following output −

type of obj: <class 'dict_items'>
dict_items([(10, 'Ten'), (20, 'Twenty'), (30, 'Thirty'), (40, 'Forty')])
update numbers dictionary
View automatically updated
dict_items([(10, 'Ten'), (20, 'Twenty'), (30, 'Thirty'), (40, 'Forty'), (50, 'Fifty')])

The keys() Method

dict 类的 keys() 方法返回 dict_keys 对象,它是字典中定义的所有键的列表。它是一个视图对象,因为每当对字典对象执行任何更新操作时,它都会自动更新。

The keys() method of dict class returns dict_keys object which is a list of all keys defined in the dictionary. It is a view object, as it gets automatically updated whenever any update action is done on the dictionary object.

Syntax

以下是 keys() 方法的语法 −

Following is the syntax of the keys() method −

Obj = dict.keys()

Return value

keys() 方法返回 dict_keys 对象,它是字典中键的视图。

The keys() method returns dict_keys object which is a view of keys in the dictionary.

Example

在这个示例中,我们创建了一个名为“numbers”的字典,其中包含整数键及其对应的字符串值。然后,我们使用 keys() 方法获取键的视图对象“obj”,并检索其类型和内容 −

In this example, we are creating a dictionary named "numbers" with integer keys and their corresponding string values. Then, we obtain a view object "obj" of the keys using the keys() method, and retrieve its type and content −

numbers = {10:"Ten", 20:"Twenty", 30:"Thirty",40:"Forty"}
obj = numbers.keys()
print ('type of obj: ', type(obj))
print (obj)
print ("update numbers dictionary")
numbers.update({50:"Fifty"})
print ("View automatically updated")
print (obj)

它将生成以下 output

It will produce the following output

type of obj: <class 'dict_keys'>
dict_keys([10, 20, 30, 40])
update numbers dictionary
View automatically updated
dict_keys([10, 20, 30, 40, 50])

The values() Method

values() 方法返回字典中所有值的视图。该对象是 dict_value 类型的,它会自动更新。

The values() method returns a view of all the values present in the dictionary. The object is of dict_value type, which gets automatically updated.

Syntax

以下是 values() 方法的语法 −

Following is the syntax of the values() method −

Obj = dict.values()

Return value

values() 方法返回一个 dict_values 视图,该视图包含字典中存在的全部值。

The values() method returns a dict_values view of all the values present in the dictionary.

Example

在下面的示例中,我们从“numbers”字典中使用 values() 方法获取值的视图对象“obj” −

In the example below, we obtain a view object "obj" of the values using the values() method from the "numbers" dictionary −

numbers = {10:"Ten", 20:"Twenty", 30:"Thirty",40:"Forty"}
obj = numbers.values()
print ('type of obj: ', type(obj))
print (obj)
print ("update numbers dictionary")
numbers.update({50:"Fifty"})
print ("View automatically updated")
print (obj)

它将生成以下 output

It will produce the following output

type of obj: <class 'dict_values'>
dict_values(['Ten', 'Twenty', 'Thirty', 'Forty'])
update numbers dictionary
View automatically updated
dict_values(['Ten', 'Twenty', 'Thirty', 'Forty', 'Fifty'])