为什么类集合对象不支持.intersection()之类的集合方法? [英] Why are set methods like .intersection() not supported on set-like objects?

查看:82
本文介绍了为什么类集合对象不支持.intersection()之类的集合方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python 3.7中,我想计算两个字典键的交集.为此,我想在其keys()上调用.intersection()方法,但是它不起作用.

In Python 3.7, I'd like to calculate the intersection of two dictionaries' keys. To do this, I'd like to call the .intersection() method on their keys(), however it does not work.

.keys()会生成一个类似于集合的对象,但是大多数集合方法对此均无效.但是,对于像&这样的集合对象,极其未知的按位运算符重载是有效的.

.keys() produces a set-like object, however most set methods don't work on it. What works however is the extremely unknown bitwise operator overloads for set-like objects, like &.

m = {'a':1, 'b':2}
n = {'b':3, 'c':4}

m.keys().intersection(n.keys())  # Pythonic, but doesn't work

m.keys() & n.keys()  # works but not readable

set(m.keys()).intersection(set(n.keys()))  # works, readable, but too verbose

我发现类似set的对象上的&重载极少使用,大多数程序员都不知道.像.intersection().union()这样的方法名称都是自记录的,并且根据此定义,它的定义肯定是Pythonic.

I find that the & overload on a set-like object is extremely rarely used and is not known by most programmers. Method names like .intersection() or .union() is self-documenting and definitely more Pythonic by this definition.

那为什么不支持呢?甚至文档都列出了&.intersection()方法(例如别名),更不用说在set-like对象上仅支持&.

Why isn't it supported then? Even the documentation lists the & and .intersection() methods like aliases, not mentioning that only & is supported on set-like objects.

注意:出于某种原因,在IPython中,自动完成功能将.isdisjoin()列出为dict.keys()上可用的方法.在这17种设置方法中,有1种.

note: For some reason, in IPython the autocomplete lists .isdisjoin() as a method which is available on dict.keys(). Out of the 17 set methods, 1 is present.

推荐答案

对于类似集合的视图,为抽象基类collections.abc.Set定义的所有操作都可用(例如,==<^).

For set-like views, all of the operations defined for the abstract base class collections.abc.Set are available (for example, ==, <, or ^).

因此,他们并没有声称匹配set甚至是frozenset,只是collections.abc.Set

So they're not claiming to match set, or even frozenset, just collections.abc.Set collections.abc.Set doesn't require any of the named methods aside from isdisjoint (which has no operator equivalent).

对于为什么 collections.abc.Set不需要命名方法,可能是因为它们有些复杂(大多数采用varargs,并且varargs可以是任何可迭代的,而不仅仅是其他集合)之类的东西,例如,您可以一次同时包含多个可迭代对象的intersection,并且他们可能想限制实现新子类(尤其是虚拟子类,它不会继承任何方法<)所需的复杂性. c12>可能选择提供).

As for why collections.abc.Set doesn't require the named methods, it may be because they are somewhat more complex (most take varargs, and the varargs can be any iterable, not just other set-like things, so you can, for example, intersection with many iterables at once), and they may have wanted to limit the complexity required to implement new subclasses (especially virtual subclasses, that wouldn't inherit any of the methods collections.abc.Set might choose to provide).

所有这些,我通常同意您的观点,即不必要地省略方法形式是不一致的.我建议您在 Python错误跟踪器上打开一个错误,以请求更改;仅仅因为它仅保证Set兼容性并不意味着它不能做更多.

All that said, I generally agree with your point that it seems needlessly inconsistent to omit the method forms. I'd recommend you open a bug on the Python bug tracker requesting the change; just because it only guarantees Set compatibility doesn't mean it can't do more.

这篇关于为什么类集合对象不支持.intersection()之类的集合方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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