为什么不是设置dict方法的方法?

发布于 2025-01-21 10:07:32 字数 957 浏览 1 评论 0原文

这些是set具有的方法,但是dict没有:

Python 3.7.3 (default, Apr 24 2020, 18:51:23) 
[Clang 11.0.3 (clang-1103.0.32.62)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from pprint import pprint
>>> pprint(set(dir(set)) - set(dir(dict)))
{'__and__',
 '__iand__',
 '__ior__',
 '__isub__',
 '__ixor__',
 '__or__',
 '__rand__',
 '__ror__',
 '__rsub__',
 '__rxor__',
 '__sub__',
 '__xor__',
 'add',
 'difference',
 'difference_update',
 'discard',
 'intersection',
 'intersection_update',
 'isdisjoint',
 'issubset',
 'issuperset',
 'remove',
 'symmetric_difference',
 'symmetric_difference_update',
 'union'}
>>> 

一些,例如set.remove,可以说> dict,但我对为什么dict没有所有公共方法的set 感到困惑。 dict的键值是否使其成为SET的重量更重? set的方法在某种意义上不是通过dict的“继承”的原因?

These are the methods that set has, but dict doesn't:

Python 3.7.3 (default, Apr 24 2020, 18:51:23) 
[Clang 11.0.3 (clang-1103.0.32.62)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from pprint import pprint
>>> pprint(set(dir(set)) - set(dir(dict)))
{'__and__',
 '__iand__',
 '__ior__',
 '__isub__',
 '__ixor__',
 '__or__',
 '__rand__',
 '__ror__',
 '__rsub__',
 '__rxor__',
 '__sub__',
 '__xor__',
 'add',
 'difference',
 'difference_update',
 'discard',
 'intersection',
 'intersection_update',
 'isdisjoint',
 'issubset',
 'issuperset',
 'remove',
 'symmetric_difference',
 'symmetric_difference_update',
 'union'}
>>> 

A few, like set.remove, arguably already exist for dict, but I am confused about why dict doesn't have all the public methods of set. Doesn't the key-value nature of dict make it jut a heavier version of set? Is there a reason methods of set are not in a sense "inherited" by dict?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

驱逐舰岛风号 2025-01-28 10:07:32

列出的所有方法(交集,symmetric_difference,...)应用于set set elements

dict < / code>中,当值可能有所不同时,这不会有意义 /添加到键上的混淆或差异。

因此,如果您想与2个字典相交,无论其值如何,最终值是什么?

d1 = {"foo":"1"}
d2 = {"foo":"2"}

D1.Intersection(D2)返回什么? D1D2?或者应该等于“工作”的交集。

如果您想与2个字典相交,请首先找出您真正想要的,然后通过与键相交(以set set s的转换)

d3 = {k:d1[k] for k in set(d1).intersection(d2)}  # or d2[k]

或与<<<<<<<<代码>项目(), set 数据的 s(规定值是可用的),执行相交并转换回字典。

d3 = dict(set(d1.items()).intersection(d2.items()))

如果两个字典中都有与不同值相关的键,则结果是不可预测的,因为键是唯一的,因此只能保留一个键(最后一个迭代的键,没有未订购的集合)

All the methods listed (intersection, symmetric_difference, ...) apply on the elements of the set.

In a dict, that wouldn't make much sense / add to the confusion to intersect or diff on the keys when the values could be possibly different.

So if you want to intersect 2 dictionnaries regardless of the values, what would the final value be?

d1 = {"foo":"1"}
d2 = {"foo":"2"}

what would d1.intersection(d2) return? d1 or d2 ? or should the values should be equal for the intersection to "work".

If you want to intersect 2 dictionnaries, first figure out what you really want, then code it either by intersecting the keys (converted as sets)

d3 = {k:d1[k] for k in set(d1).intersection(d2)}  # or d2[k]

or extract the tuples with items(), make sets of data (provided that dict values are hashable), perform intersection and convert back to dictionary.

d3 = dict(set(d1.items()).intersection(d2.items()))

if there are keys in both dictionaries associated with different values, the result is pretty unpredictable because keys are unique so only one would be kept (the last which is iterated upon, with sets which are unordered)

半寸时光 2025-01-28 10:07:32

最近讨论了这一点,请参阅有关实施set的完整API 在PEP 584的这一部分中

This has been discussed recently, see the discussion on implementing set’s full API in this section of PEP 584.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文