简体   繁体   中英

Python matching partial strings in list elements between two lists

In my code I am trying to match the item from 'match' to the the strings in the 'data' list.

I want the code to look at the first word in 'match' list and if it matches with a string in the data 'list' then that will be added to another list. A second check I want to do is if the first 2 words in 'match' list match with the strings in data.

Currently my output is giving me only one instance of water12 - whereas both shoud have been picked up.

Please could someone let me know where I might be going wrong?


match =['f helo','happy hellp','floral', 'alpha','12133','water12 puppies']
data=['f we are', 'hello there', 'alpha beta','happy today is the case','112133 is it', 'floral is my fave', 'water12 if healthy','water12 puppies are here and exist']

lst=[]
for i in match:
    for j in data:
        if i.split()[0] in j:
            lst.append(j)
            data.remove(j)
            break
        if len(i) > 1:
            k= ' '.join(i.split()[:2])
            if k in j:
                lst.append(j) 
                data.remove(j)
                break
                
    else:
        lst.append(i + ' - not found')

print(lst)

Desired output:

output= [ 'f we are', 'alpha beta','happy today is the case','112133 is it', 'floral is my fave', 'water12 if healthy','water12 puppies are here and exist']

You don't want to remove elements from the list you are iterating in. Instead, you can add a condition to verify if the matched word has already been added to your output list.

It should be something like:

lst = []
for i in match:
    has_match = False
    for j in data:
        if i.split()[0] in j:
            has_match = True
            print(i, j)
            if j not in lst:
                lst.append(j)
        if len(i) > 1:
            k = ' '.join(i.split()[:2])
            if k in j:
                has_match = True
                print(i, j)
                if j not in lst:
                    lst.append(j)
    if not has_match:
        lst.append(i + ' - not found')

I also removed the break keywords, since they may stop your code from finding matches in multiple strings in data . Using a boolean should do the work. Let us know if you have further questions.

尝试使用列表理解:

output = [x for x in data if any(True if z in x else False for z in x for y in match)]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM