matching similar elements in between two lists

瘦欲@ 提交于 2019-12-12 18:44:25

问题


I'm new to python so apologies if its a silly question.

I have two lists
L1=['marvel','audi','mercedez','honda'] and

L2=['marvel comics','bmw','mercedez benz','audi'].

I want to extract matching elements which contains in list L2 matched with list L1. So what I done :

for i in L1:
   for j in L2:
       if j in i:
          print (j)

  output is ['audi']

But, I also wants to return elements if its also consist any word match like mercedez in mercedez benz and marvel in marvel comics. so final output would be:

j=['audi','mercedez benz','marvel comics']

回答1:


I think what you really want here is the elements of L2 that contain any elements in L1. So simply replace if j in i with if i in j:

for i in L1:
   for j in L2:
       if i in j:
          print (j)

This outputs:

marvel comics
audi
mercedez benz



回答2:


If you would like to use regular expressions then you can do:

import re
re.findall(".*|".join(L1),"\n".join(L2))

['marvel comics', 'mercedez benz', 'audi']



回答3:


This is one approach using str.startswith

Ex:

L1=['marvel','audi','mercedez','honda']  
L2=['marvel comics','bmw','mercedez benz','audi']
res = []
for i in L2:
    for j in L1:
        if i.startswith(j):
            res.append(i)
print(res)

Output:

['marvel comics', 'mercedez benz', 'audi']

Using in

Ex:

res = []
for i in L2:
    for j in L1:
        if j in i:
            res.append(i)
print(res)



回答4:


a performant approach would be to build a "flat" dictionary with each word as key and the relevant group of words as value.

L2=['marvel comics','bmw','mercedes benz','audi']    
match_dict = {k:v for v in L2 for k in v.split()}

which is:

{'audi': 'audi',
 'benz': 'mercedes benz',
 'bmw': 'bmw',
 'comics': 'marvel comics',
 'marvel': 'marvel comics',
 'mercedes': 'mercedes benz'}

now scan the first list and issue element if in dictionary:

L1=['marvel','audi','mercedes','honda']
result = [match_dict[x] for x in L1 if x in match_dict]

result:

['marvel comics', 'audi', 'mercedes benz']

once the dictionary is built, you can scan large lists with high performance (O(1) lookup)




回答5:


Using list comprehension:

[j for i in L1 for j in L2 if (j.startswith(i))]

['marvel comics', 'audi', 'mercedez benz']



回答6:


Make some changes in your code

for i in L2:
    for j in L1:
        if j in i:
            print (i)


来源:https://stackoverflow.com/questions/51493185/matching-similar-elements-in-between-two-lists

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!