比较两个列表:
abc=[[a,b],[c],[x,y,z]]
pqr=[a,b,x,y,z]
打印匹配的字符串。
我希望输出如下:
z=[a,b,x,y,z]
答案 0 :(得分:0)
尝试以下代码:
abc=[['a','b'],['c'],['x','y','z']]
pqr = ['a','b','x','y','z']
z=[c for l in abc for c in l if c in pqr]
输出:
['a', 'b', 'x', 'y', 'z']
答案 1 :(得分:0)
您可以尝试,这也是
from itertools import chain
abc=[['a','b'],['c'],['x','y','z','k']]
pqr = ['a','b','x','y','z','k','l']
result=[x for x in pqr if x in list(chain.from_iterable(abc))]
print result
<强>输出强>
['a', 'b', 'x', 'y', 'z', 'k']