Python创建自己的字典子集的字典视图
正如 SO 上关于该主题的许多问题所证明的那样,获取字典的一部分是一项非常常见的任务,并且有一个相当好的解决方案:
{k:v for k,v in dict.viewitems() if some_test(k,v)}
但这会创建一个具有自己的映射的新字典。对于许多操作来说,最好有一个原始字典的不可变视图(即它不支持视图上的赋值或删除操作)。实现这样的类型可能很容易,但是本地实用程序类的激增并不好。
所以,我的问题是:是否有一种内置的方法来获取这样的“子集视图”?或者是否有第三方库(最好通过 PyPi 提供)可以提供此类实用程序的良好实现?
As the many questions on the topic here on SO attest, taking a slice of a dictionary is a pretty common task, with a fairly nice solution:
{k:v for k,v in dict.viewitems() if some_test(k,v)}
But that creates a new dictionary, with its own mappings. For many operations, it would be nice to just have an immutable view of the original dict (i.e. it does not support assignment or deletion operations on the view). Implementing such a type is probably easy, but it's not good to have a proliferation of local utility classes.
So, my question is: is there a built-in way of obtaining such a "subset view"? Or is there a third-party library (preferably available via PyPi) that provides a good implementation of such a utility?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
似乎没有内置的方法来获取字典的视图。最简单的解决方法似乎是 Jochen 的方法。我稍微修改了他的代码以使其适合我的目的:
因此,由于不同的 __repr__() 方法,因此 d2 在除打印之外的所有方面都表现得像字典。从
dict
继承来获取__repr__()
需要重新实现每个方法,就像collections.OrderedDict
所做的那样。如果只想要一个只读视图,可以继承collections.Mapping
并保存__setitem__()
和__delitem__()
的实现。我发现DictView
对于从self.__dict__
选择参数并以紧凑的形式传递它们很有用。There seems to be no builtin way to obtain a view into a dictionary. The easiest workaround appears to be Jochen's approach. I adapted his code slightly to make it work for my purposes:
So
d2
behaves like a dictionary in all aspects except for printing, due to the different__repr__()
method. Inheriting fromdict
to get__repr__()
would require reimplementation of each and every method, as is done forcollections.OrderedDict
. If one wants only a readonly view, one can inherit fromcollections.Mapping
and save the implementation of__setitem__()
and__delitem__()
. I findDictView
useful to select parameters fromself.__dict__
and pass them on in a compact form.这很容易实现:
This is pretty easy to implement:
为了澄清语义,您正在考虑这样的事情:?
如果是这样,那么我不知道有这样的第三方课程。如果您想让其余方法的实现更容易一些,您可以考虑使用“UserDict”作为基类,它基本上只是字典的包装器(“UserDict.data”属性用于存储包装的字典) 。
To clarify the semantics, you're thinking of something like this:?
If so, then I don't know of any such third party class. If you want to make implementing the remaining methods a little easier, you might look at using "UserDict" as a base class, which is basically just a wrapper for dict (the "UserDict.data" attribute is used to store the wrapped dict).