Python:看看一个集合是否完全包含另一个集合?

发布于 2024-08-31 10:08:18 字数 182 浏览 2 评论 0原文

有没有一种快速方法来检查一组是否完全包含另一组?

像这样的东西:

>>> set([1, 2, 3]).containsAll([2, 1])
True

>>> set([1, 2, 3]).containsAll([3, 5, 9])
False

Is there a fast way to check if one set entirely contains another?

Something like:

>>> set([1, 2, 3]).containsAll([2, 1])
True

>>> set([1, 2, 3]).containsAll([3, 5, 9])
False

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

永言不败 2024-09-07 10:08:19

您可以使用 set.issubset()< /a> 或 set.issuperset()< /a> (或其基于运算符的对应项:<=>=)。请注意,这些方法将接受任何可迭代作为参数,而不仅仅是集合:

>>> {1, 2}.issubset([1, 2, 3])
True
>>> {1, 2, 3}.issuperset([1, 2])
True

但是,如果您使用运算符,则两个参数都必须是集合:

>>> {1, 2} <= {1, 2, 3}
True
>>> {1, 2, 3} >= {1, 2}
True

You can use either set.issubset() or set.issuperset() (or their operator based counterparts: <= and >=). Note that the methods will accept any iterable as an argument, not just a set:

>>> {1, 2}.issubset([1, 2, 3])
True
>>> {1, 2, 3}.issuperset([1, 2])
True

However, if you use operators, both arguments must be sets:

>>> {1, 2} <= {1, 2, 3}
True
>>> {1, 2, 3} >= {1, 2}
True
旧时光的容颜 2024-09-07 10:08:19

如果您怀疑一个集合是另一个集合的子集,并将这两个集合相交,则如果它是子集,则结果等于其自身。

a = [2,1,3,3]
b = [5,4,3,2,1]
set(a).intersection(set(b)) == set(a)
>>True

If you suspect a set to be a subset of another, and intersect those two sets together, the result is equal to itself if it is a subset.

a = [2,1,3,3]
b = [5,4,3,2,1]
set(a).intersection(set(b)) == set(a)
>>True
爱要勇敢去追 2024-09-07 10:08:19

一个选项保持不变 - 减法:

>>> {1, 2} - {1, 2, 3}
set([])
>>> {1, 2, 3} - {1, 2}
set([3])

基本上,您检查第一个列表中的哪些元素不在第二个列表中。

我发现它非常方便,因为您可以显示缺少哪些值:

>>> def check_contains(a, b):
...     diff = a - b
...     if not diff:
...         # All elements from a are present in b
...         return True
...     print('Some elements are missing: {}'.format(diff))
...     return False
...
>>> check_contains({1, 2}, {1, 2, 3})
True
>>> check_contains({1, 2, 3}, {1, 2})
Some elements are missing: set([3])
False

One option is left untouched -- subtraction:

>>> {1, 2} - {1, 2, 3}
set([])
>>> {1, 2, 3} - {1, 2}
set([3])

Basically, you check what elements in the first list are not in the second.

I found it very handy since you could show what values are missing:

>>> def check_contains(a, b):
...     diff = a - b
...     if not diff:
...         # All elements from a are present in b
...         return True
...     print('Some elements are missing: {}'.format(diff))
...     return False
...
>>> check_contains({1, 2}, {1, 2, 3})
True
>>> check_contains({1, 2, 3}, {1, 2})
Some elements are missing: set([3])
False
独木成林 2024-09-07 10:08:19
>>> set([1,2,3]).issuperset(set([2,1]))
True
>>> set([1,2,3]).issuperset(set([3,5,9]))
False
>>> set([1,2,3]).issuperset(set([2,1]))
True
>>> set([1,2,3]).issuperset(set([3,5,9]))
False
倾城°AllureLove 2024-09-07 10:08:19

如果主列表不完全包含子列表,下面的函数返回 0;如果完全包含,则返回 1。

def islistsubset(sublist,mainlist):
     for item in sublist:
             if item in mainlist:
                     contains = 1
             else:
                     contains = 0
                     break;
     return contains

Below function return 0 if mainlist doesn't contains sublist fully and 1 if contains fully.

def islistsubset(sublist,mainlist):
     for item in sublist:
             if item in mainlist:
                     contains = 1
             else:
                     contains = 0
                     break;
     return contains
稚然 2024-09-07 10:08:18

这些是列表,但如果您真的指的是集合,您可以使用 issubset 方法。

>>> s = set([1,2,3])
>>> t = set([1,2])
>>> t.issubset(s)
True
>>> s.issuperset(t)
True

对于列表,您将无法比检查每个元素更好。

Those are lists, but if you really mean sets you can use the issubset method.

>>> s = set([1,2,3])
>>> t = set([1,2])
>>> t.issubset(s)
True
>>> s.issuperset(t)
True

For a list, you will not be able to do better than checking each element.

三五鸿雁 2024-09-07 10:08:18

为了完整起见:这相当于 issubset (尽管可以说有点不太明确/可读):

>>> set([2,1]).issubset(set([1,2,3]))
True
>>> set([2,1]) <= set([1,2,3])
True

>>> set([3,5,9]).issubset(set([1,2,3]))
False
>>> set([3,5,9]) <= set([1,2,3])
False

For completeness: this is equivalent to issubset (although arguably a bit less explicit/readable):

>>> set([2,1]).issubset(set([1,2,3]))
True
>>> set([2,1]) <= set([1,2,3])
True

>>> set([3,5,9]).issubset(set([1,2,3]))
False
>>> set([3,5,9]) <= set([1,2,3])
False
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文