python - 为什么在 Python 中使用 {1} == frozenset({1})?

标签 python

我觉得奇怪的是,在 Python 中,{1} == frozenset({1}) 的计算结果为 Truesetfrozenset 是不同的对象类型,我没有看到其他可迭代对象类型之间的这种相似性(例如 {1} == (1,) 的计算结果为 False)。为什么集合会出现这种行为?是否有其他具有类似行为的可迭代对象类型?

最佳答案

根据 documentation python2documentation python3

Instances of set are compared to instances of frozenset based on their members. For example, "set('abc') == frozenset('abc')" returns True.

在 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/

相关文章:

python - 我们可以改变训练模型中的 input_length 吗?

python - 在 Python 中估计图像的亚像素值

python - C - 导入 Python 模块

python - 根据多个条件在 Pandas 数据框中创建一个新列

python - PyQt5 TableModel(QAbstractTableModel) 显示没有数据

python - 比较大于 > 或小于 < 的两个列表

python - 如何返回给定特定值的字典中的先前值?

javascript - 变量没有符号的语言如何处理动态调度/调用?

python - 本地设置的 Cython 编译器指令会影响一个或所有功能吗?

python - python 的近似最近邻库(WINDOWS)