用于查看Python词典的用例 [英] Usecases for views into Python dictionaries

查看:52
本文介绍了用于查看Python词典的用例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读什么是Python字典视图对象? ...性能折衷是什么?? 以及有关Python字典视图的官方文档,我仍然缺少一个不错的用例,这使我理解它们为什么会存在.

After reading What are Python dictionary view objects? and … what are the performance tradeoffs …? and the official documentation about views on Python dictionaries, I still lack a decent usecase which makes me understand why they are there.

这个想法是在字典上有一个视图,它在创建键/值/项目列表方面有两个主要区别:①创建速度更快(在恒定时间内),并且内存占用量恒定,并且②它反映了创建字典后,字典也会发生变化.

The idea is to have a view on the dictionary which has two major differences to creating a list of the keys/values/items: ① It's faster created (in constant time) and with constant amount of memory footprint and ② it reflects changes in the dictionary also after its creation.

这很好并且可以理解,但是我不知道何时使用它会有用. 毕竟,无论我在哪里传递这样的观点,我都可以自己传递字典.它也将在恒定时间内传递,而不会占用额外的内存,并且也会反映出自身的变化(当然).无论我想对视图执行什么操作,我也可以对传递的字典本身进行处理.还是我错过了什么?

This is fine and understood but I lack an idea when it would be useful to have this. After all wherever I pass such a view, I could pass the dictionary itself instead. It will also be passed in constant time without additional memory footprint and it will also reflect changes on itself (of course). Whatever I would want to do with the view I could also do with the passed dictionary itself. Or am I missing something?

有人提到更改字典时在字典上使用iteritems()(在Python3中为iter())会使迭代器无效.这是真实的.不幸的是,对于在视图上进行迭代创建的任何迭代器,同样如此.所以再次没有区别.

Someone mentioned that using iteritems() (iter() in Python3) on a dictionary while it was changing would invalidate the iterator. This is true. Unfortunately this is likewise true for any iterator created to iterate over a view. So again there is no difference.

那么使用视图有什么好处?是否有实际的用例,超出了上面链接的较旧问题中的范围?

So what's the benefit of using a view? Is there any practical usecase which goes beyond what can be found in the older questions linked above?

推荐答案

这是官方文档 PEP 3106 是受 Java集合框架启发的提案.

Here is the official documentation, and PEP 3106 is the proposal inspired by the Java Collections Framework.

字典视图由Python 3中的dict.keys()dict.values()dict.items()返回.回溯到Python 2.7后,必须直接调用dict.viewkeys()dict.viewvalues()dict.viewitems().

Dictionary views are returned by dict.keys(), dict.values() and dict.items() in Python 3. Backported to Python 2.7, you must directly call dict.viewkeys(), dict.viewvalues() and dict.viewitems().

字典视图像dicts一样可迭代,并且它们增加了内存改进,因为它们在迭代时不建立列表.这些特权是微妙的,并融入了语言中.

Dictionary views are iterable like dicts, and they add memory improvements as they do not build lists when iterated. These perks are subtle and baked into the language.

一个直接的用例来自于字典键视图的设置方式.

从文档中

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

keys & {'eggs', 'bacon', 'salad'}
# {'bacon'}

keys ^ {'sausage', 'juice'}
# {'juice', 'sausage', 'bacon', 'spam'}

除了正常的设置操作外,此行为还简化了测试两个词典是否共享公用密钥的操作.

In addition to normal set operations, this behavior simplifies testing whether two dictionaries share common keys.

请参阅Brandon Rhodes的 PyCon 2017演讲,更强大的词典 关于此主题.

See Brandon Rhodes' PyCon 2017 talk, The Dictionary Even Mightier on this topic.

这篇关于用于查看Python词典的用例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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