我觉得奇怪的是,在 Python 中,{1} == frozenset({1})
的计算结果为 True
。 set
和 frozenset
是不同的对象类型,我没有看到其他可迭代对象类型之间的这种相似性(例如 {1} == (1,)
的计算结果为 False
)。为什么集合会出现这种行为?是否有其他具有类似行为的可迭代对象类型?
最佳答案
根据 documentation python2和 documentation python3
Instances of
set
are compared to instances offrozenset
based on their members. For example, "set('abc') == frozenset('abc')
" returnsTrue
.
在 python3 文档中:
Both set and frozenset support set to set comparisons. Two sets are equal if and only if every element of each set is contained in the other (each is a subset of the other). A set is less than another set if and only if the first set is a proper subset of the second set (is a subset, but is not equal). A set is greater than another set if and only if the first set is a proper superset of the second set (is a superset, but is not equal).
关于python - 为什么在 Python 中使用 {1} == frozenset({1})?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51407264/