简体   繁体   中英

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, eg:

When I try to input the keyword teacher and sales

input keywords: teacher sales

it should return result like this:

  • schoolteacher
  • mathematics teacher
  • salesperson
  • sales manager

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..?

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)

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!

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)

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

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