0
我的问题的答案可能在这里附近,但我找不到它。如何通过比较两个其他列表与字符串匹配来构建第三个列表。
我有两个列表:
['batman','superman','spiderman',]
['batman','ironman','superman','flash','wonderwoman']
我想比较这两个列表,并返回匹配的元素作为第三列表:
['batman','superman']
我只找到这个解决方案:
list=['a cat','a dog','a yacht']
string='a cat'
if string in list:
print 'found a cat!'
但它只是字符串和列表之间的比较...
来吧,你没有搜索那么多。有很多重复。单行:'set(list1).intersection(list2)'是一个。 –