📌  相关文章
📜  Python|检查一个列表是否是其他列表的子集

📅  最后修改于: 2022-05-13 01:54:49.254000             🧑  作者: Mango

Python|检查一个列表是否是其他列表的子集

有时我们会遇到检查一个列表是否只是列表的扩展,即是否只是一个列表的超集的问题。这类问题在竞争性编程中非常流行。有它的速记有助于事业。让我们讨论实现这一特定任务的各种方法。

方法 #1:使用all()函数

all() 用于在一行中检查容器的所有元素。检查一个列表的所有元素是否存在于其他列表中。

Python3
# Python3 code to demonstrate
# to check if list is subset of other
# using all()
 
# initializing list
test_list = [9, 4, 5, 8, 10]
sub_list = [10, 5, 4]
 
# printing original lists
print("Original list : " + str(test_list))
print("Original sub list : " + str(sub_list))
 
# using all() to
# check subset of list
flag = 0
if(all(x in test_list for x in sub_list)):
    flag = 1
 
# printing result
if (flag):
    print("Yes, list is subset of other.")
else:
    print("No, list is not subset of other.")


Python3
# Python3 code to demonstrate
# to check if list is subset of other
# using issubset()
 
# initializing list
test_list = [9, 4, 5, 8, 10]
sub_list = [10, 5]
 
# printing original lists
print("Original list : " + str(test_list))
print("Original sub list : " + str(sub_list))
 
# using issubset() to
# check subset of list
flag = 0
if(set(sub_list).issubset(set(test_list))):
    flag = 1
 
# printing result
if (flag):
    print("Yes, list is subset of other.")
else:
    print("No, list is not subset of other.")


Python3
# Python3 code to demonstrate
# to check if list is subset of other
# using intersection()
 
# initializing list
test_list = [9, 4, 5, 8, 10]
sub_list = [10, 5]
 
# printing original lists
print("Original list : " + str(test_list))
print("Original sub list : " + str(sub_list))
 
# using intersection() to
# check subset of list
flag = 0
if((set(sub_list) & set(test_list)) == set(sub_list)):
    flag = 1
 
# printing result
if (flag):
    print("Yes, list is subset of other.")
else:
    print("No, list is not subset of other.")


Python3
# Python3 code to demonstrate
# to check if list is subset of other
 
# Importing
from collections import Counter
 
 
def checkInFirst(a, b):
     # getting count
    count_a = Counter(a)
    count_b = Counter(b)
 
    # checking if element exists in second list
    for key in count_b:
        if key not in count_a:
            return False
        if count_b[key] > count_b[key]:
            return False
    return True
 
 
# initializing list
a = [1, 2, 4, 5]
b = [1, 2, 3]
 
# Calling function
res = checkInFirst(a, b)
 
# Printing list
print("Original list : " + str(a))
print("Original sub list : " + str(b))
 
if res == True:
    print("Yes, list is subset of other.")
else:
    print("No, list is not subset of other.")
 
# Added by Paras Jain(everythingispossible)


Python3
# Python3 code to demonstrate
# to check if list is subset of other
 
# initializing list
one = [1, 2, 3, 4, 5]
two = [1, 2]
 
# using set to find if element exists in another list
result = set(x in one for x in two)
 
flag = 0
 
for ans in result:
    if ans == False:
        flag = 1
 
# Printing list
print("Original list : " + str(one))
print("Original sub list : " + str(two))
 
if flag == 0:
    print("Yes, list is subset of other.")
else:
    print("No, list is not subset of other.")
 
# Added by Paras Jain(everythingispossible)


Python3
from functools import reduce
from operator import and_ 
 
# main containing checker
def contains(superset, subset) -> bool:
     
    # creates a list of boolean values and
    # combines them using the and operator
    return reduce(and_, [i in superset for i in subset])
 
 
# creating some lists for testing
superset = [3, 4, 5, 6]
subset = [4, 5]
not_subset = [4, 5, 7]
 
# print whether or not the
# subset is in the superset
print(f"{subset} is in {superset}: {contains(superset,
subset)}")
print(f"{not_subset} is in {superset}: {contains(superset,
not_subset)}")


输出 :

Original list : [9, 4, 5, 8, 10]
Original sub list : [10, 5, 4]
Yes, list is subset of other.

方法 #2:使用set.issubset()函数

检查子列表的最常用和推荐的方法。这个函数是为执行检查一个列表是否是另一个列表的子集而量身定制的。

Python3

# Python3 code to demonstrate
# to check if list is subset of other
# using issubset()
 
# initializing list
test_list = [9, 4, 5, 8, 10]
sub_list = [10, 5]
 
# printing original lists
print("Original list : " + str(test_list))
print("Original sub list : " + str(sub_list))
 
# using issubset() to
# check subset of list
flag = 0
if(set(sub_list).issubset(set(test_list))):
    flag = 1
 
# printing result
if (flag):
    print("Yes, list is subset of other.")
else:
    print("No, list is not subset of other.")

输出 :

Original list : [9, 4, 5, 8, 10]
Original sub list : [10, 5]
Yes, list is subset of other.

方法#3:使用set.intersection()函数

另一种处理集合的方法,该方法检查两个列表的交集是否最终成为我们正在检查的子列表。这证实了一个列表是另一个列表的子集。

Python3

# Python3 code to demonstrate
# to check if list is subset of other
# using intersection()
 
# initializing list
test_list = [9, 4, 5, 8, 10]
sub_list = [10, 5]
 
# printing original lists
print("Original list : " + str(test_list))
print("Original sub list : " + str(sub_list))
 
# using intersection() to
# check subset of list
flag = 0
if((set(sub_list) & set(test_list)) == set(sub_list)):
    flag = 1
 
# printing result
if (flag):
    print("Yes, list is subset of other.")
else:
    print("No, list is not subset of other.")

输出 :

Original list : [9, 4, 5, 8, 10]
Original sub list : [10, 5]
Yes, list is subset of other.

方法#4:使用迭代和计数器

使用两个列表中的元素计数来检查第二个列表是否是第一个列表的子集。

Python3

# Python3 code to demonstrate
# to check if list is subset of other
 
# Importing
from collections import Counter
 
 
def checkInFirst(a, b):
     # getting count
    count_a = Counter(a)
    count_b = Counter(b)
 
    # checking if element exists in second list
    for key in count_b:
        if key not in count_a:
            return False
        if count_b[key] > count_b[key]:
            return False
    return True
 
 
# initializing list
a = [1, 2, 4, 5]
b = [1, 2, 3]
 
# Calling function
res = checkInFirst(a, b)
 
# Printing list
print("Original list : " + str(a))
print("Original sub list : " + str(b))
 
if res == True:
    print("Yes, list is subset of other.")
else:
    print("No, list is not subset of other.")
 
# Added by Paras Jain(everythingispossible)

输出 :

Original list : [1, 2, 4, 5]
Original sub list : [1, 2, 3]
No, list is not subset of other.

方法#5:使用设置索引

Python3

# Python3 code to demonstrate
# to check if list is subset of other
 
# initializing list
one = [1, 2, 3, 4, 5]
two = [1, 2]
 
# using set to find if element exists in another list
result = set(x in one for x in two)
 
flag = 0
 
for ans in result:
    if ans == False:
        flag = 1
 
# Printing list
print("Original list : " + str(one))
print("Original sub list : " + str(two))
 
if flag == 0:
    print("Yes, list is subset of other.")
else:
    print("No, list is not subset of other.")
 
# Added by Paras Jain(everythingispossible)

输出 :

Original list : [1, 2, 3, 4, 5]
Original sub list : [1, 2]
Yes, list is subset of other.

方法 #6:使用 functools.reduce

Python3

from functools import reduce
from operator import and_ 
 
# main containing checker
def contains(superset, subset) -> bool:
     
    # creates a list of boolean values and
    # combines them using the and operator
    return reduce(and_, [i in superset for i in subset])
 
 
# creating some lists for testing
superset = [3, 4, 5, 6]
subset = [4, 5]
not_subset = [4, 5, 7]
 
# print whether or not the
# subset is in the superset
print(f"{subset} is in {superset}: {contains(superset,
subset)}")
print(f"{not_subset} is in {superset}: {contains(superset,
not_subset)}")

输出:

[4, 5] is in [3, 4, 5, 6]: True
[4, 5, 7] is in [3, 4, 5, 6]: False