[英]In Python, how can I check if an array contains all elements of another array/list, including duplicates?
[英]How to check if a list contains all the elements of another list INCLUDING duplicates
例如
t = ["A", "A", "A", "b", "b"]
s = ["A", "A", "b", "b"]
s 不包含 t 中的每个元素。 t 包含 s 中的每个元素。
我想出了这个,但想知道是否有更有效的方法
def check(l1, l2):
for i in l2:
if(l2.count(i) > l1.count(i)):
return False
return True
重复调用list.count
(对于同一个列表)非常低效。 您可以使用collections.Counter
以及它如何实现差异:
from collections import Counter
def contains(l1, l2):
return not (Counter(l2) - Counter(l1))
>>> contains(["A", "A", "A", "b", "b"], ["A", "A", "b", "b"])
True
>>> contains(["a", "a", "b"], ["a", "b"])
True
>>> contains(["a", "b"], ["a", "a", "b"])
False
>>> contains(["a", "a", "b"], ["a", "b", "c"])
False
这是一种基于计数器的方法:
from collections import Counter
counter_s = Counter(s)
all(counter_s.get(key_t, 0) >= count_t
for key_t, count_t in Counter(t).items())
这可能比看起来要棘手一些。 你给出的例子有点人为,例如在你有的复杂情况下怎么样:
t = ["A", "A", "A", "b", "b", "m"]
s = ["A", "A", "b", "b", "z"]
t
和s
都不s
彼此的子集。 因此,如果您想查找s
是否是t
的子集(或者换句话说s
包含在t
包括重复项),问题有两个检查:
s
是否具有t
所有唯一元素t
的s
每个元素,其在s
频率需要小于或等于在t
找到的对应频率这是一个可以解决您的问题的示例函数:
from collections import Counter
def is_subset(A: list[str], B: list[str]) -> bool:
"""is_subset tests if all elements of A
are included in B.
Args:
A (list[str]): List of string elements.
B (list[str]): List of string elements.
Returns:
bool: True if all elements of A (inc. duplicates) are in B.
"""
# Create counter objs
c_A = Counter(A)
c_B = Counter(B)
# First check to make sure unique elements are a subset
# Or it fails at this stage
uniq_A = c_A.keys()
uniq_B = c_B.keys()
if uniq_A <= uniq_B:
return bool(c_B - c_A) if c_B != c_A else True
return False
if __name__ == '__main__':
s = ["A", "A", "b", "b"]
t = ["A", "A", "b", "b", "b"]
k = ["A", "b", "z"]
print(is_subset(t, t)) # True
print(is_subset(k, s)) # False
print(is_subset(s, t)) # True
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.