3

AKA the correct version of this:

if ['hi', 'hello', 'greetings'] in userMessage:
    print('Hello!')

I tried what's showed above but it says it cannot use lists, it must use a single string. Same thing if I set the array to an object/variable. If I use "or" it doesn't seem to work altogether.

Amir
  • 10,600
  • 9
  • 48
  • 75
Gabe Weiner
  • 41
  • 1
  • 4

3 Answers3

8

If the goal is just to say if any of the known list appears in userMessage, and you don't care which one it is, use any with a generator expression:

if any(srchstr in userMessage for srchstr in ('hi', 'hello', 'greetings')):

It will short-circuit when it gets a hit, so if hi appears in the input, it doesn't check the rest, and immediately returns True.

If the words must be found as individual words (so userMessage = "This" should be false, even though hi appears in it), then use:

if not {'hi', 'hello', 'greetings'}.isdisjoint(userMessage.split()):

which also short-circuits, but in a different way; it iterates userMessage.split() until it matches one of the keywords, then stops and returns False (which the not flips to True), returning True (flipped to False by not) only if none of the words matches a keyword.

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
2

You can do:

found = set(['hi','hello','greetings']) & set(userMessage.split())
for obj in found:
    print found

if you are looking for multiple words as well

TheoretiCAL
  • 19,461
  • 8
  • 43
  • 65
  • 1
    `set` is already a builtin Python type. Just use `set(lst)`, and then a set intersection is a good way to go for it. – Alex Huszagh Jan 05 '16 at 01:07
  • 1
    Also, one more thing before I give it a "+1", cause this the correct answer, line one of your code snippet has a SyntaxError due the trailing `:` – Alex Huszagh Jan 05 '16 at 01:15
  • 2
    @AlexanderHuszagh: You could also avoid converting the `list` to a `set` at all (and use literals, because why make a `list` only to immediately convert it to a `set`) with `found = {'hi','hello','greetings'}.intersection(userMessage.split())`; the named methods accept any iterable, not just sets, and (implementation detail) don't convert it to a `set` even internally, instead just iterating it and membership testing against the original `set` to determine if it belongs in the result `set`. – ShadowRanger Aug 14 '19 at 17:16
0

You can also compare multiple elements using Set:

if set(['hi', 'hello', 'greetings']) <= set(userMessage.split()):
   print("Hello!")

But be careful to use split(), once it will not avoid ponctuation. So, if your userMessage is something like "hi, hello, greetings." it will compare the words against ["hi,", "hello,", "greetings."]

  • This tests if *all* of the words are in `userMessage`, not just one; the OP [specifically asked for it to be true "if one of them was present print the result"](https://stackoverflow.com/questions/34602571/check-if-python-input-contains-a-list-of-keywords/34602889#comment56951130_34602571). To make it work for just one, you'd need something like `if not {'hi', 'hello', 'greetings'}.isdisjoint(userMessage.split()):`; `isdisjoint` returns `True` when there is no overlap, so flipping it (with `not`) tells you at least one was present. – ShadowRanger Aug 14 '19 at 17:03