6

As part of a project I have to be able to identify keywords that a user would input.

For example if I type "how to i find London" it would see the words London and find.

How would I do this using an array to make the code look cleaner.

city = [London, Manchester, Birmingham]
where = input("Where are you trying to find")
  if(city in where):
    print("drive 5 miles")
  else:
    print("I'm not to sure")

So I just want to know how do I find words from an array within a user input.

This isn't the project but a similar way of doing it.

2

3 Answers 3

8

You are on the right track. The first change is that your string literals need to be inside of quotes, e.g. 'London'. Secondly you have your in backwards, you should use element in sequence so in this case where in cities.

cities = ['London', 'Manchester', 'Birmingham']
where = input("Where are you trying to find")
if where in cities:
    print("drive 5 miles")
else:
    print("I'm not to sure")

If you want to do substring matching, you can change this to

cities = ['London', 'Manchester', 'Birmingham']
where = input("Where are you trying to find")
if any(i in where for i in cities ):
    print("drive 5 miles")
else:
    print("I'm not to sure")

This would accept where to be something like

'I am trying to drive to London'
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your help, this seems to be working great
@CoryKramer: Good Answer, !! one suggestion if any(i in where for i in city): should be if any(i in where for i in cities): right ? As the python version is not mention, what about 2.x ? should use raw_input instead of input ?!
2
cities = ['London', 'Manchester', 'Birmingham']
where = raw_input("Where are you trying to find")
for city in cities:
    if city in where:
        print("drive 5 miles")
        break
else:
    print("I'm not to sure")

It will check user input is present in a list or not

Comments

1

You can try this:

cities = ['London', 'Manchester', 'Birmingham']
where = input("Where are you trying to find")
    if(any(city in where for city in cities)):
        print("drive 5 miles")
    else:
        print("I'm not to sure")

Note the minor changes to your code.

The any method returns true if ANY of the values in the received array are true. So we create an array searching for every city in the user input, if any of then is true, it return true.

2 Comments

If you leave the inner list comprehension [] it will evaluate all of the cities before passing to the any function. If you remove them, the expression inside any is a generator expression and can short-circuit upon finding the first match for performance.
That is true, I will update my answer, thank you for the the sugestion!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.