lundi 20 août 2018

python if multiple string return the words that contains in the sentences

I have a list of words and I want to do if statement, below is my list:

list = ['camera','display','price','memory'(will have 200+ words in the list)]

Here is my code:

def check_it(sentences):

    if 'camera' in sentences and 'display' in sentences:
        return "Camera/Display"
    if 'camera' in sentences and 'display' in sentences or 'price' in sentences:
        return "Camera/Display/price"
    ...
    return "Others"

h.loc[:, 'Category'] = h.Mention.apply(check_it)

There will be too many combinations for these and also I want to have the words return to row individually. Does anyone know how to make this sample and return the word individually instead of doing 'camera/display/price'?

Aucun commentaire:

Enregistrer un commentaire