简体   繁体   English

在 Python 中,如何检查一个数组是否包含另一个数组/列表的所有元素,包括重复项?

[英]In Python, how can I check if an array contains all elements of another array/list, including duplicates?

There appear to be several ways to determine if a set is a subset of another set, but I haven't been able to find something concise that determines whether all elements of a list (or array), including duplicate values, appear in another list (or array).似乎有几种方法可以确定一个集合是否是另一个集合的子集,但我无法找到简洁的方法来确定列表(或数组)的所有元素(包括重复值)是否出现在另一个列表中(或数组)。 For example, for the hypothetical function contains_all(A, B) which checks whether all elements of B are contained in A , these are some expected outputs:例如,对于假设的 function contains_all(A, B)检查B的所有元素是否包含在A中,这些是一些预期的输出:

contains_all([11, 4, 11, 6], [6, 11]) returns True (order doesn't matter) contains_all([11, 4, 11, 6], [6, 11])返回True (顺序无关紧要)

contains_all([11, 4, 11, 6], [11, 9]) returns False (no 9) contains_all([11, 4, 11, 6], [11, 9])返回False (否 9)

contains_all([11, 4, 11, 6], [11, 11]) returns True contains_all([11, 4, 11, 6], [11, 11])返回True

contains_all([11, 4, 11, 6], [11, 11, 11]) returns False (only two 11's) contains_all([11, 4, 11, 6], [11, 11, 11])返回False (只有两个 11)

contains_all([11, 4, 11, 6], [6, 6]) returns False (only one 6) contains_all([11, 4, 11, 6], [6, 6])返回False (只有一个 6)

contains_all([11, 4, 11, 6], [11, 4, 6, 11]) returns True contains_all([11, 4, 11, 6], [11, 4, 6, 11])返回True

contains_all([11, 4, 11, 6], [11, 4, 11, 6, 5]) returns False (no 5) contains_all([11, 4, 11, 6], [11, 4, 11, 6, 5])返回False (没有 5)

The fourth and fifth examples above, specifically, are what I'm having trouble implementing.具体来说,上面的第四个和第五个示例是我在实施时遇到的问题。 set(B).issubset(A) or list comprehensions cover the other cases, but not these, since sets don't have duplicate elements. set(B).issubset(A)或列表推导涵盖了其他情况,但不包括这些,因为集合没有重复的元素。 Is there a concise way to do this?有没有一种简洁的方法来做到这一点? If not, what would be the best way to approach writing a function that does this?如果不是,那么编写这样的 function 的最佳方法是什么? It seems something like this may be possible with collections.Counter objects or multisets, but I'm not sure how to go about it. collections.Counter 对象或多重集似乎可能会发生这样的事情,但我不确定如何 go 来解决它。

In Python 3.10, collections.Counter has this functionality built-in:在 Python 3.10 中, collections.Counter内置了以下功能:

def contains_all(A, B):
    return Counter(A) >= Counter(B)

However, due to supporting negative counts, >= needs to iterate over all keys in both counters , which is inefficient when you know all counts are nonnegative.但是,由于支持负计数, >=需要遍历两个 counter 中的所有键,当您知道所有计数都是非负数时,这是低效的。 Manually checking just the keys from the B counter (as in Guy's answer ) should be faster.手动检查 B 计数器中的键(如Guy 的回答)应该更快。 (I don't have a Python 3.10 handy, so I can't provide timings.) (我手边没有 Python 3.10,所以我无法提供时间。)

You are correct, collections.Counter is a good way to go.你是对的, collections.Counter是 go 的好方法。 You just need to go over the B counter and check if the the value is smaller or equal.您只需要通过B计数器 go 并检查该值是否小于或等于。 Do it in all() to check all the key-value pairsall()中执行以检查所有键值对

def contains_all(a, b):
    counter_a = Counter(a)
    counter_b = Counter(b)
    return all(v <= counter_a[k] for k, v in counter_b.items())

Edit编辑

user2357112 answer is much nicer, but apply to Pyton3.10 or newer. user2357112 的答案要好得多,但适用于 Pyton3.10 或更高版本。 For older versions you can use this answer.对于旧版本,您可以使用此答案。

Try this尝试这个

def contains(A, B):
contains_all = []
data_missing = []
for i in B:
    if i in A and B.count(i) <= A.count(i):
        contains_all.append(i)
    else:
        data_missing.append(i)
if data_missing:
    return False
else:
    return True

>>> print(contains([11, 4, 11, 6], [11, 4, 11, 6, 5]))
>>> False

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查一个列表是否包含另一个列表的所有元素,包括重复项 - How to check if a list contains all the elements of another list INCLUDING duplicates 如何检查一个数组是否包含另一个数组的所有元素? 如果不是,则输出缺失的元素 - How to check if an array contains all the elements of another array? If not, output the missing elements Python:在另一个数组(包括重复数组)中查找数组所有元素的索引 - Python: finding index of all elements of array in another including repeating arrays 如何检查一个列表是否包含另一个列表的 2 个元素? - How do I check if one list contains 2 elements of another list? 如何检查数组的所有元素是否都在Python中填充 - How to check if all elements of array are filled in Python 如何检查数组是否在 3 旁边包含 3? - How can I check if an array contains a 3 next to a 3? 检查列表数组是否包含另一个列表中的元素 - Check if list array contains element in another list 如何检查numpy数组的所有元素是否在另一个numpy数组中 - How to check if all elements of a numpy array are in another numpy array 检查一个数组的所有元素是否在另一个数组中 - Check if all elements of one array is in another array 如何检查列表中一行的任何部分是否包含另一个列表的整行? PYTHON - How can I check if any part of a line from a list contains the full line of another list? PYTHON
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM