我有两个列表,如何在te
和inc
inc=[['A', 'B'], ['C', 'D'], ['E', 'F'], ['G', 'H']]
te=['A', 'B']
print te in inc # return True
inc=[['A', 'B'], ['C', 'D'], ['E', 'F'], ['G', 'H']]
te=['A', 'C', 'B']
print te in inc # return False
答案 0 :(得分:2)
在这里摩擦我的水晶球,你需要一个扁平的数据结构(set
是理想的,因为你不关心订单,只关心会员资格)和all
:
s_inc = set(c for sublist in inc for c in sublist)
s_inc
Out[7]: set(['H', 'G', 'F', 'E', 'D', 'C', 'B', 'A'])
te=['A', 'C', 'B']
all(c in s_inc for c in te)
Out[9]: True
答案 1 :(得分:0)
不确定这是否正是您尝试做的,但请尝试:
inc = [['A', 'B'], ['C', 'D'], ['E', 'F'], ['G', 'H']]
te = ['A', 'C', 'B']
def flatten(target):
return_list = list()
for element in target:
if isinstance(element,list):
return_list.extend(flatten(element))
else:
return_list.append(element)
return return_list
set_inc = set(flatten(inc))
set_te = set(te)
print(set_te.issubset(set_inc)) # True