5

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.

Dain Wareing
  • 53
  • 1
  • 1
  • 4
  • 1
    *the words London and find* - why "find" ? If I entered "Birmingham-London" ? – RomanPerekhrest Apr 12 '17 at 12:29
  • Possible duplicate of [How do I check if an input is in a list in python?](http://stackoverflow.com/questions/11185781/how-do-i-check-if-an-input-is-in-a-list-in-python) – WhatsThePoint Apr 12 '17 at 12:33

3 Answers3

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'
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
  • 1
    Thank you for your help, this seems to be working great – Dain Wareing Apr 12 '17 at 12:42
  • @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 ?! – Surajano Apr 12 '17 at 12:59
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

mahendra kamble
  • 1,375
  • 1
  • 14
  • 24
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.

DSLima90
  • 2,680
  • 1
  • 15
  • 23
  • 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. – Cory Kramer Apr 12 '17 at 12:56
  • That is true, I will update my answer, thank you for the the sugestion! – DSLima90 Apr 12 '17 at 13:30