list1 = [[['a','b'],['c','d']],[['f','g'],['h','i']],[['j','k','l'], ['a','b']]]
list2 = [['a','b'],['c','d'],['f','g'],['h','i']]
所以在列表1中,有3个列表。我想检查一下列表1中的列表列表是否是列表2的子集,但是列表列表中的所有列表都必须在列表2中,这样才能得到正确的结果。如果所有内容都在列表2中,则为true;如果不是每个列表,则为false
[['a','b'],['c','d']]
[['f','g'],['h','i']]
[['j','k','l'], ['a','b']]
所以情况如下
These are from list1, and we're checking against list2
Both [a, b] and [c, d] should be in list 2 -> Both are in list 2, so Return True
Both [f, g] and [h, i] should be in list 1 -> Both are in list 2, so return true
Both [j, k, l] and [a, b] should be in list 1 -> f, k, l is not in list 2, so return False even though a, b are in list 2
Here is my desired output for above results
[True, True, False]
或者
val1 = True
val2 = True
val3 = False
代码
def xlist(list1, list2):
if all(letter in list1 for letter in list2):
print('True')
print xlist(list1, list2)
final = []
"""I am checking i in list1. In actual, I should be checking all lists within the list of list1."""
for i in list1:
print(xlist(list1, list2))
final.append(xlist(list1, list2))
print(final)
您的问题是没有从
xlist
函数返回任何值(print
与return
不同)。更改为:然后:
结果是:
作为另一种较短的方法,可以将^{} 函数与嵌套的list comprehension一起使用:
相关问题 更多 >
编程相关推荐