什么是字典视图对象? [英] What are dictionary view objects?

查看:87
本文介绍了什么是字典视图对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在python 2.7中,我们获得了词典查看方法可用.

In python 2.7, we got the dictionary view methods available.

现在,我知道以下内容的优缺点:

Now, I know the pro and cons of the following:

  • dict.items()(和valueskeys):返回一个列表,因此您可以实际存储结果,并且
  • dict.iteritems()(等等):返回一个生成器,因此您可以逐个迭代生成的每个值.
  • dict.items() (and values, keys): returns a list, so you can actually store the result, and
  • dict.iteritems() (and the like): returns a generator, so you can iterate over each value generated one by one.

dict.viewitems()(等等)有什么作用?他们有什么好处?它是如何工作的?毕竟是什么看法?

What are dict.viewitems() (and the like) for? What are their benefits? How does it work? What is a view after all?

我读到视图总是反映字典中的变化.但是,从性能和内存的角度来看,它的表现如何?优点和缺点是什么?

I read that the view is always reflecting the changes from the dictionary. But how does it behave from the perf and memory point of view? What are the pro and cons?

推荐答案

字典视图本质上就是它们的名字:视图就像一个窗口,关于字典.以下摘录自官方文档(针对Python 3) :

Dictionary views are essentially what their name says: views are simply like a window on the keys and values (or items) of a dictionary. Here is an excerpt from the official documentation for Python 3:

>>> dishes = {'eggs': 2, 'sausage': 1, 'bacon': 1, 'spam': 500}
>>> keys = dishes.keys()
>>> values = dishes.values()

>>> # view objects are dynamic and reflect dict changes
>>> del dishes['eggs']
>>> keys  # No eggs anymore!
dict_keys(['sausage', 'bacon', 'spam'])

>>> values  # No eggs value (2) anymore!
dict_values([1, 1, 500])

(Python 2等效项使用dishes.viewkeys()dishes.viewvalues().)

(The Python 2 equivalent uses dishes.viewkeys() and dishes.viewvalues().)

此示例显示了视图的动态特征:键视图不是给定时间点的键副本,而是一个简单的窗口,该视图显示了您的钥匙;如果它们被更改,那么您在窗口中看到的内容也会发生更改.在某些情况下,此功能很有用(例如,可以在程序的多个部分中使用键的视图,而不必每次都需要重新计算当前键列表)—请注意,如果修改了字典键在视图上进行迭代时,迭代器的行为方式尚不明确,可能会导致错误.

This example shows the dynamic character of views: the keys view is not a copy of the keys at a given point in time, but rather a simple window that shows you the keys; if they are changed, then what you see through the window does change as well. This feature can be useful in some circumstances (for instance, one can work with a view on the keys in multiple parts of a program instead of recalculating the current list of keys each time they are needed)—note that if the dictionary keys are modified while iterating over the view, how the iterator should behave is not well defined, which can lead to errors.

优点之一是,例如,看键的 仅使用少量固定的内存,并且需要少量固定的处理器时间,因为没有创建键列表(另一方面,Python 2经常不必要地创建一个新列表,如Rajendran T所引用的那样),该列表占用的内存和时间与长度成正比.列表).要继续进行窗口类比,如果您想查看墙后的风景,只需在其中开一个洞(您就可以创建一个窗口)了;将关键帧复制到列表中将相当于在墙上绘制风景的副本-该副本需要时间,空间并且不会自我更新.

One advantage is that looking at, say, the keys uses only a small and fixed amount of memory and requires a small and fixed amount of processor time, as there is no creation of a list of keys (Python 2, on the other hand, often unnecessarily creates a new list, as quoted by Rajendran T, which takes memory and time in an amount proportional to the length of the list). To continue the window analogy, if you want to see a landscape behind a wall, you simply make an opening in it (you build a window); copying the keys into a list would correspond to instead painting a copy of the landscape on your wall—the copy takes time, space, and does not update itself.

总而言之,视图只是…字典上的视图(窗口),即使字典发生更改,视图也仍会显示它们的内容.它们提供了与列表不同的功能:键列表包含给定时间点的字典键的副本,而视图是动态的,并且获取视图的速度更快,因为它确实如此无需复制任何数据(键或值)即可创建.

To summarize, views are simply… views (windows) on your dictionary, which show the contents of the dictionary even after it changes. They offer features that differ from those of lists: a list of keys contain a copy of the dictionary keys at a given point in time, while a view is dynamic and is much faster to obtain, as it does not have to copy any data (keys or values) in order to be created.

这篇关于什么是字典视图对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆