1

I know this question is too much basic, but I am unable to understand it. For example, if have to search any of N words in a string, how can I do this. I tried logical operations.

('browser' or 'chrome') in 'start game'

It will return false.

('browser' or 'chrome') in 'start chrome'

It will return True.

('browser' or 'chrome') in 'start chrome'

This should return True, but it returns false. chrome word is in the string, so why it returns false.

('browser' and 'chrome') in 'start chrome'

This returns True. But why it returns true, even if only one word matches.

I want to return True, if even one-word matches, no matter at which index it is.

decorator-factory
  • 2,733
  • 13
  • 25
Talha Anwar
  • 2,699
  • 4
  • 23
  • 62
  • 3
    `('browser' or 'chrome')` evaluates to `'browser'`. You can use `if any(x in 'start chrome' for x in ('browser', 'chrome'))` – khelwood May 09 '20 at 11:23
  • 3
    See [How do “and” and “or” act with non-boolean values?](https://stackoverflow.com/questions/47007680/how-do-and-and-or-act-with-non-boolean-values) – khelwood May 09 '20 at 11:26
  • any([a for a in ['browser',"chrome"] if a in 'start chrome']) – Kurohige May 09 '20 at 11:28

3 Answers3

4

('browser' or 'chrome') evaluates to 'browser'.

See How do “and” and “or” act with non-boolean values?

You can use the any function:

if any(x in 'start chrome' for x in ('browser', 'chrome')):
    # at least one of the words is in the string 

and the all function:

if all(x in 'start chrome' for x in ('browser', 'chrome')):
    # both words are in the string
khelwood
  • 55,782
  • 14
  • 81
  • 108
2

Suppose we want to compute a or b. The or operator works like this:

  1. If a is truthy, return a
  2. Otherwise, return b.

If you have more than two operands, for example, a or b or c or d, it's equivalent to a or (b or (c or d)), so it will work according to this algorithm:

  1. If a is truthy, return a
  2. Otherwise, if b is truthy, return b
  3. Otherwise, if c is truthy, return c
  4. Otherwise, return d.

So this expression: ('browser' or 'chrome') in 'start chrome' will not work as you expect it to. First, ('browser' or 'chrome') will evaluate to 'browser' (because 'browser' is truthy, as is any non-empty string), and 'browser' in 'start chrome' is False.

What you might want to use is:

('browser' in 'start chrome') or ('chrome' in 'start chrome')

Or you can use any, coupled with a generator comprehension:

string = 'start chrome'
if any(substring in string for substring in ['browser', 'chrome', 'chromium']):
    print('Ta da!')

Or, if you really like map,

if any(map(string.__contains__, ['browser', 'chrome', 'chromium'])):
    print('Ta da!')

Basically, any returns True if at least one element of the given iterable is truthy.

a and b and c works similarly to or: 1. If a is falsey, return a 1. Otherwise, if b is falsey, return b 1. Otherwise, return c

What you might want to use in place of your and-y expression is:

('start' in 'start chrome') and ('chrome' in 'start chrome')

Or you can use all, which is like any, but implements and-logic instead or or-logic: https://docs.python.org/3/library/functions.html#all

decorator-factory
  • 2,733
  • 13
  • 25
0

There's probably 2 approaches you could go with: An easy, less dynamic approach:

"browser" in "start chrome" or "chrome" in "start chrome"

A longer but much more dynamic approach:

def OneContains(l, text):
    for i in l:
        if i in text:
            return True
    return False


print(OneContains(["browser","chrome"],"start chrome"))
Skalex
  • 136
  • 2
  • 10