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

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

问题描述

在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()(和 ):返回一个列表,因此您可以实际存储结果

  • dict.iteritems()(等):返回一个生成器,所以你可以迭代逐个生成的值。

  • dict.items() (and values, keys): returns a list, so you can actually store the result
  • dict.iteritems() (and the like): returns a generator, so you can iterable 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?

我看到,视图总是反映从字典的变化。但从perf和memory的角度来看,它是如何表现的呢?什么是pro和cons?

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).

一个优点是,在 中,例如,键仅使用小而固定的内存量,并且需要一个小而固定的处理器时间量,因为没有创建一个键列表(另一方面,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.

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

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