Tri
Tri

Reputation: 3039

Python search multiple word in list

I have Python list like this:

['assistant manager', 'salesperson', 'doctor', 'production manager', 'sales manager', 'schoolteacher', 'mathematics teacher']

Now I want to search with multiple keywords in my list, e.g:

When I try to input the keyword teacher and sales

input keywords: teacher sales

it should return result like this:

So far I have made a code like this:

job_list = ['assistant manager', 'salesperson', 'doctor', 'production manager', 'sales manager', 'schoolteacher', 'mathematics teacher']

def search_multiple_words(search_words):
    search_words = [search_words]

    for line in job_list:
        if any(word in line for word in search_words):
            print(line)

search_words = input("input keywords: ")
search_multiple_words(search_words)

But it just works when I input one keyword, not multiple keywords like example I gave above.

So, how to do that..?

Upvotes: 2

Views: 3816

Answers (4)

Sssssuppp
Sssssuppp

Reputation: 711

You can use the find() method. This should work for you:

lis=['assistant manager', 'salesperson', 'doctor', 'production manager', 'sales manager', 'schoolteacher', 'mathematics teacher']
str1 = ["teacher", "sales"]
x=[]
for y in lis:
    for string in str1:
        if y.find(string) != -1:
            x.append(y)

The above code was for readability and understandability. However, it can be compressed into a nice one-liner using list comprehensions like this:

x = [y for y in lis for string in str1 if y.find(string) != -1]

Both do the same thing and give the output as this

['salesperson', 'sales manager', 'schoolteacher', 'mathematics teacher']

Good Luck!

Upvotes: 1

YusufUMS
YusufUMS

Reputation: 1493

You can try this

job_list = ['assistant manager', 'salesperson', 'doctor', 'production manager', 
            'sales manager', 'schoolteacher', 'mathematics teacher']

def search_multiple_words(search_words):
    search_words = [search_words]

    for line in job_list:
        if any(word in line for word in search_words):
            print(line)

search_words = input("input keywords: ").split(' ')
for w in search_words:
    search_multiple_words(w)

output:

input keywords: sales teacher
salesperson
sales manager
schoolteacher
mathematics teacher

Upvotes: 1

kerwei
kerwei

Reputation: 1842

As Adrian Shum said, your input gives you a single string. You need to split it up first before feeding it to your function

job_list = ['assistant manager', 'salesperson', 'doctor', 'production manager', 'sales manager', 'schoolteacher', 'mathematics teacher']

def search_multiple_words(search_words):
    # Not neecssary anymore as you're feeding a list
    # search_words = [search_words]

    for line in job_list:
        if any(word in line for word in search_words):
            print(line)

search_words = input("input keywords: ").strip().split()
search_multiple_words(search_words)

Upvotes: 1

InfinityAndBeyond
InfinityAndBeyond

Reputation: 36

job_list = ['assistant manager', 'salesperson', 'doctor', 'production manager',
            'sales manager', 'schoolteacher', 'mathematics teacher']


def search_multiple_words(search_words):
    search_words = search_words.split(' ')

    out = [s for s in job_list if any(xs in s for xs in search_words)]
    print(out)


search_words = input("input words: ")
search_multiple_words(search_words)

Upvotes: 2

Related Questions