首页 > 解决方案 > Python issubset 在一组集合上

问题描述

我有一套:

>>> x = set()
>>> x.add(frozenset({1}))
>>> x.add(frozenset({2}))
>>> x.add(frozenset({3}))
>>> x
{frozenset({2}), frozenset({3}), frozenset({1})}

我有一个候选集:

>>> y = frozenset({2})
>>> y
frozenset({2})

我想知道我的候选集是否在我的集合中:

>>> y.issubset(x)
False
>>> 

为什么这会返回 False?我应该如何确定我的候选集是否在我的集合中?

标签: pythonset

解决方案


x = set()
x.add(frozenset({1}))
x.add(frozenset({2}))
x.add(frozenset({3}))
print(x)

y = frozenset({2})
print(y in x) # check if candidate set is in set of sets

这应该返回 True


推荐阅读