为什么不是设置dict方法的方法?
这些是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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
列出的所有方法(交集,symmetric_difference,...)应用于
set set
的 elements 。在
dict < / code>中,当值可能有所不同时,这不会有意义 /添加到键上的混淆或差异。
因此,如果您想与2个字典相交,无论其值如何,最终值是什么?
D1.Intersection(D2)
返回什么?D1
或D2
?或者应该等于“工作”的交集。如果您想与2个字典相交,请首先找出您真正想要的,然后通过与键相交(以
set set
s的转换)或与<<<<<<<<代码>项目(), set 数据的 s(规定值是可用的),执行相交并转换回字典。
如果两个字典中都有与不同值相关的键,则结果是不可预测的,因为键是唯一的,因此只能保留一个键(最后一个迭代的键,没有未订购的集合)
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?
what would
d1.intersection(d2)
return?d1
ord2
? 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
set
s)or extract the tuples with
items()
, makeset
s of data (provided that dict values are hashable), perform intersection and convert back to dictionary.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)
最近讨论了这一点,请参阅有关实施
set
的完整API 在PEP 584的这一部分中。This has been discussed recently, see the discussion on implementing
set
’s full API in this section of PEP 584.