[英]Is there a Python datatype for unordered sets of sets?
I want to store sets A
and B
, in a set C
. 我想将集合
A
和B
存储在集合C
。 My condition is that if set A = {a,b}
and set B = {b,a}
我的条件是,如果设置
A = {a,b}
且设置B = {b,a}
Then set A
is equal to set B
然后集合
A
等于集合B
Set C
will then be = {{a,b}}
集
C
将为= {{a,b}}
Is there a datatype which supports something like this? 是否有支持这种数据类型的数据类型?
The frozenset()
type meets your criteria: frozenset()
类型符合您的条件:
>>> A = frozenset({"a", "b"})
>>> B = frozenset({"b", "a"})
>>> A == B
True
>>> frozenset({ A, B })
frozenset({frozenset({'b', 'a'})})
Yes, and that datatype is just set
. 是的,而该数据类型只是
set
。 The trick is that the inside sets have to be frozenset
s, the immutable counterpart of set
, for them to be placed inside another set. 诀窍是, 里面套必须
frozenset
秒,一成不变的副本set
,让他们放在另一组内。 You may also want to make the outer set
a frozenset
, depending on what you want to do with it; 您可能还想将外部
set
frozenset
set
,具体取决于您要如何处理它。 for example, if you want to stick these sets inside more layers of sets indefinitely, it's probably easier to go with frozensets for everything. 例如,如果您想将这些集合无限期地粘贴在更多的集合层中,则对所有内容使用冻结集合可能更容易。
>>> A = frozenset(['a', 'b'])
>>> B = frozenset(['b', 'a'])
>>> C = {A, B}
>>> C
set([frozenset(['a', 'b'])])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.