什么是 Python 字典视图对象?

programmingpythonserver side programming

字典是一种用于存储一组对象的数据结构。字典具有一组键,每个键都有一个分配的值。当提供键时,字典将返回分配的值。

在 Python 字典中,一些方法返回视图对象。此视图对象是动态视图对象,这意味着当我们在字典中做出一些更改时,视图会反映所有这些更改。

Python 中字典的主要视图对象是键、值和项。它们提供字典条目的非恒定视图。

键包含字典的所有键,值对象包含所有值,而项包含键值对。

视图对象键表示字典中的所有键。您可以使用 keys() 方法检索此对象的内容。

如果您打印视图对象键,它将按插入顺序显示字典中所有 的新列表。

一旦我们使用 keys() 方法将所有键检索到一个变量(称为 keys)中,如果我们更新字典(即添加或删除键),则更改将反映在变量中。

示例 1

在下面的例子中,我们尝试使用 keys() 方法检索字典的所有键。

d = {'a': 5, 'b': 2, 'c': 0, 'd': 7} k = d.keys() print(k) print(type(k))

输出

dict_keys(['a', 'b', 'c', 'd'])
<class 'dict_keys'>

示例 2

更新将反映在检索到的键中

在下面的程序中,我们创建了一个包含 4 个键值对的字典。从字典中检索键并将其存储在变量键中。

  • 后来我们向字典中添加了一个元素。

  • 如果在更新后打印变量 keys 的值。您可以在那里找到添加元素的键。

dictionary = {'a': 5, 'b': 2, 'c': 0, 'd': 7} keys = dictionary.keys() print("Keys before Dictionary Updation:", keys) # updating the dictionary dictionary['e'] = 5 print("listing updated dictionary:", dictionary) # dynamic view object is updated automatically print("Keys after Dictionary Updation:", keys)

输出

Keys before Dictionary Updation: dict_keys(['a', 'b', 'c', 'd'])
listing updated dictionary: {'a': 5, 'b': 2, 'c': 0, 'd': 7, 'e': 5}
Keys after Dictionary Updation: dict_keys(['a', 'b', 'c', 'd', 'e'])

示例 3

以下是 keys() 函数的另一个示例,在这里我们删除一个密钥并验证检索到的密钥的内容。

dict = {'Madhu': 1001, 'Raju': 1002, 'Radha': 2003, 'Yadav': 9004} print("The dictionary before removing a key is : " + str(dict)) removed_value = dict.pop('Raju') print("The value of removed key is : " + str(removed_value)) # Printing dictionary after removal print("The dictionary after removing key is : " + str(dict))

输出

The dictionary before removing a key is : {'Madhu': 1001, 'Raju': 1002, 'Radha': 2003, 'Yadav': 9004}
The value of removed key is : 1002
The dictionary after removing key is : {'Madhu': 1001, 'Radha': 2003, 'Yadav': 9004}

values

values() 是 Python 编程语言的内置方法,它返回一个视图对象。该视图对象以列表形式包含字典的值。

示例 1

以下是打印字典值的示例 -

d = {'a': 5, 'b': 2, 'c': 0, 'd': 7} v = d.values() print(v) print(type(v))

输出

正如我们在以下输出中看到的,字典的所有值都会被打印出来 -

dict_values([5, 2, 0, 7])
<class 'dict_values'>

示例 2- 更新字典

以下是通过向字典中添加值来更新字典的示例 -

d = {'a': 5, 'b': 2, 'c': 0, 'd': 7} v = d.values() print('values before before updating : ', v) d['e'] = 5 print('Dictionary after adding new value', d) # dynamic view object is updated automatically print('value of the updated dictionary: ', v)

输出

values before before updating : dict_values([5, 2, 0, 7]) Dictionary after adding new value {'a': 5, 'b': 2, 'c': 0, 'd': 7, 'e': 5} value of the updated dictionary: dict_values([5, 2, 0, 7, 5])

示例 3

以下是总结字典值的示例 -

#Adding the values in the dictionary d1 = {"apple" : 5000, "gold" : 60000, "silver" : 50000} list1 = d1.values() a=(sum(list1)) print('Total value:',a)

输出

Total value: 115000

项目

items() 方法返回字典项目((键,值)对)的新视图,作为包含所有字典键和值的列表。

语法

以下是项目的语法 −

dictionary.items()

示例 1

以下是打印字典所有项目的示例 −

d = {'a': 5, 'b': 2, 'c': 0, 'd': 7} i = d.items() print(i) print(type(i))

输出

dict_items([('a', 5), ('b', 2), ('c', 0), ('d', 7)])
<class 'dict_items'>

示例 2

以下是从字典中删除项目的示例 -

d1 = {'a': 5, 'b': 2, 'c': 0, 'd': 7} # Printing all the Dictionarie’s item print("Original Dictionary items:", d1) items = d1.items() # Delete an item from dictionary del[d1['c']] #Printing Updtaed dictionary print('Updated Dictionary:',items)

输出

Original Dictionary items: {'a': 5, 'b': 2, 'c': 0, 'd': 7}
Updated Dictionary: dict_items([('a', 5), ('b', 2), ('d', 7)])

示例 3

以下示例显示了字典的 items() 的修改 −

car = { "brand": "Ford", "model": "Mustang", "year": 1964 } print('Orginal dictionary: ',car) x = car.items() car["year"] = 2018 print('updated dictionary: ',x)

输出

正如我们在以下输出中看到的,年份已从 1964 年修改为 2018 年 −

Orginal dictionary: {'brand': 'Ford', 'model': 'Mustang', 'year': 1964}
updated dictionary: dict_items([('brand', 'Ford'), ('model', 'Mustang'), ('year', 2018)])

相关文章