我正在尝试检查一个小列表中是否包含另一个更大列表中存在的所有数字。
情况1:- 列表1:[97,97,196],列表2:[97,97,101,103,196]
- 这应该返回True,因为列表1中的所有元素已经在列表2中了
- 这应该返回False,因为列表1有两个97,而列表2只包含一个97
list1 = [97,97,196]
list2 = [97,97,99,101,103,196]
def isConsist(list1,list2):
check = False
# Iterate in the 1st list
for m in list1:
# Iterate in the 2nd list
for n in list2:
# if there is a match
if m == n:
check = True
list2.remove(n) // remove the found element
else:
check = False
return False
return check
check = isConsist(list1,list2)
print(check)
这是我的代码,但它没有正确工作。
我的代码得到了错误的结果,因为在检查第一个列表中的196时,它与第二个列表中的99进行比较,然后返回False。