比较两个列表(其中一个列表与另一个列表)并返回匹配的索引

时间:2016-12-08 12:44:32

标签: python-2.7

a = ['french', 'english']

b = ['(portuguese; french)', '(english)']

我想比较两个列表,作为回报,我想要匹配的索引

我试过了:

matches = list([i for i, item in enumerate(a) if item in b]+[i for i, item in enumerate(b) if item in a])

但结果是一个空列表

1 个答案:

答案 0 :(得分:0)

你需要这个吗?

b = ['(portuguese, french)', '(english)']
a = ['french', 'english']
matching = [i for i, x in enumerate(b) if any(thing in x for thing in a)]
print matching


output: [0, 1]