[英]Add the empty set to a family of sets in a frozenset in python
Suppose I generate a frozenset with 假设我用
A = frozenset(frozenset([element]) for element in [1,2,3])
And I have the empty set 我有空集
E = frozenset(frozenset())
Now I want to have the union of both sets: 现在,我想同时拥有两个集合:
U = A | E
This gives me 这给我
frozenset({frozenset({2}), frozenset({3}), frozenset({1})})
this assumes, that a frozenset, containing an empty frozenset is itself empty. 这假定包含空冻结集的冻结集本身就是空的。 However, I'd like to have 但是,我想要
frozenset({frozenset({}), frozenset({2}), frozenset({3}), frozenset({1})})
So, I'd like to add the empty set explicitly to the set. 因此,我想将空集显式添加到集合中。 This is, for example, necessary in my opinion when constructing a power set? 例如,在构建电源集时,我认为这是必要的吗?
So: Is a family of sets that only contains the empty set itself empty? 那么:一个仅包含空集本身为空的集集是否是空的? Is there a way, in Python, to explicitly include an empty set into a family of sets using the variable types set
and frozenset
? 在Python中,有没有一种方法可以使用变量类型set
和frozenset
将空集显式地包含到一组set
?
E
is an empty set, with no elements: E
是一个空集,没有任何元素:
>>> frozenset(frozenset())
frozenset()
That's because the argument to fronenset()
is an iterable of values to add . 这是因为fronenset()
的参数是fronenset()
的可迭代值 。 frozenset()
is an empty iterable, so nothing is added. frozenset()
是一个空的可迭代对象,因此未添加任何内容。
If you expected E
to be a set with one element, you need to pass in an iterable with one element; 如果您期望E
是一个包含一个元素的集合,则需要传入一个包含一个元素的可迭代对象。 use {...}
set notation, or a single element tuple (...,)
, or a list [...]
: 使用{...}
设置符号,或单个元素元组(...,)
或列表[...]
:
>>> frozenset({frozenset()}) # pass in a set() with one element.
frozenset({frozenset()})
Now you get your expected output: 现在,您将获得预期的输出:
>>> A = frozenset(frozenset([element]) for element in [1,2,3])
>>> E = frozenset({frozenset()})
>>> A | E
frozenset({frozenset({3}), frozenset({2}), frozenset({1}), frozenset()})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.