Home > database >  Pyhton: Remove strings from list that doesn't contain specific string but not exact match
Pyhton: Remove strings from list that doesn't contain specific string but not exact match

Time:12-16

I have a list of strings

e.g:

kw_list = 
       ['facebook',
        'google',
        'bank',
        'bank cd rates', 
        'forever 21', 
        'bank rates',
        'bank of america mortgage rates',
        'bank exchange rates']

and I have a string

e.g:

keyword = 'bank rates'

I want to be able to remove anything from the list that doesn't contain my string in this case bank rates. But I don't want to remove it as an exact match.

So after cleaning the list it would look something like this:

kw_list = [
        'bank cd rates', 
        'bank rates',
        'bank of america mortgage rates',
        'bank exchange rates']

I tried using:

new_kws = []
     for i in kw_list:
                if keyword in i:
                    new_kws.append(i)
                    print('It has the word',keyword , '--->' ,i)
                else:
                    print('it doesnt have the word ---> ', i)

But this only looks for the exact match of 'bank rates' and in the following example this code will remove strings like bank cd rates, bank of america mortgage rates and bank exchange rates' So it will leave the list as:

   kw_list = [
            'bank rates',
           ]

What would be the best way of achieving what I want?

CodePudding user response:

You can use:

new_list = [word for word in kw_list if all(val in word for val in keyword.split(' '))]

OUTPUT

['bank cd rates', 'bank rates', 'bank of america mortgage rates', 'bank exchange rates']

CodePudding user response:

I tried something like this, by splitting and checking each word in list:

def check_keyword(kw_string, kw_split):
    word_list = kw_string.split(' ')
    for word in kw_split:
        if word not in word_list:
            return False
    return True
    
kw_list = ['facebook',
        'google',
        'bank',
        'bank cd rates', 
        'forever 21', 
        'bank rates',
        'bank of america mortgage rates',
        'bank exchange rates']
        
        
keyword = 'bank rates'
kw_split = keyword.split(' ')

result = [x for x in kw_list if check_keyword(x, kw_split)]

print(result)

CodePudding user response:

Same code as yours except one line of condition is changed, this will work as per your expectation

    kw_list = [
            'bank cd rates',
            'bank rates',
            'bank of america mortgage rates',
            'bank exchange rates']
    keyword = 'bank rates'
    new_kws = []
    for i in kw_list:
        if "bank" in i and "rates" in i:#changed condition
            new_kws.append(i)
            print('It has the word',keyword , '--->' ,i)
        else:
            print('it doesnt have the word ---> ', i)
    print(new_kws)

  • Related