matching similar elements in between two lists

2019-07-08 21:20发布

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']

6条回答
Rolldiameter
2楼-- · 2019-07-08 21:26

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']
查看更多
Lonely孤独者°
3楼-- · 2019-07-08 21:33

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)

查看更多
【Aperson】
4楼-- · 2019-07-08 21:34

Using list comprehension:

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

['marvel comics', 'audi', 'mercedez benz']
查看更多
欢心
5楼-- · 2019-07-08 21:40

Make some changes in your code

for i in L2:
    for j in L1:
        if j in i:
            print (i)
查看更多
Explosion°爆炸
6楼-- · 2019-07-08 21:42

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)
查看更多
太酷不给撩
7楼-- · 2019-07-08 21:44

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
查看更多
登录 后发表回答