【发布时间】:2021-07-15 11:36:13
【问题描述】:
我有两个列表,一个包含人们姓氏的列表,另一个包含类似数据的列表。我已经使用any() 来匹配两个列表并输出匹配项。
提供的示例数据,实际列表包含数千个条目。
matchers = ['Balle', 'Jobson', 'Watts', 'Dallow', 'Watkins']
full_name = ['Balle S & R', 'Donald D & S', 'Watkins LTD', 'Balle R & R', 'Dallow K & C']
matching = [s for s in full_name if any(xs in s for xs in matchers)]
print(matching)
我想返回每个匹配的索引。对于上面的例子,理想的输出是:
[0, 0], [4, 2], [0, 3], [3, 4]
我试过了:
print([[i for i in range(len(full_name)) if item1 == full_name[i]] for item1 in matchers])
但这会返回一个空数组列表。实际上,我的列表包含数千个条目。当匹配的数据不完全相同时,是否可以找到匹配的索引?
【问题讨论】: