2

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:

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

Tri
  • 2,722
  • 5
  • 36
  • 65
  • 1
    Quite a lot of issue: 1. `input()` give you one string, you should have split the input search words into a list. 2. first 2 lines in `search_multiple_words()` seems not making any sense – Adrian Shum Feb 22 '19 at 01:44

4 Answers4

2
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)
1

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!

Sssssuppp
  • 683
  • 1
  • 7
  • 29
1

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)
kerwei
  • 1,822
  • 1
  • 13
  • 22
1

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
YusufUMS
  • 1,506
  • 1
  • 12
  • 24