Let's say you have a company's information like this:
companies = [
['zmpEVqsbCUO1aXStxHkSVA', 'palms-car-wash'],
['5T0vKfIJWP1xTnxA7fJ17w', 'meat-and-bread'],
['C0d5kzUx6C19mLcxQyhxCA', 'alamo-drafthouse-cinema-'],
['ch1ercqwoNLpQLxpTb90KQ', 'boston-tea-stop']
]
Let's say you want to exclude some business if any string/substring of a list is present in some information of the list above:
no_interest = ['museum', 'cinema', 'car']
I have done this, (we only look in the 2nd column of every entry):
# KEEPING ONLY RESULTS WHERE WE DO NOT FIND THE SUBSTRINGS
[x for x in companies if (no_interest[0] not in x[1]) & (no_interest[1] not in x[1]) & (no_interest[2] not in x[1])]
# Returns
[['5T0vKfIJWP1xTnxA7fJ17w', 'meat-and-bread'],
['ch1ercqwoNLpQLxpTb90KQ', 'boston-tea-stop']]
It seems to work even if I would prefer it to work with an 'OR' statement instead of an 'AND' (&) which for me is a cumulative operator and should be working if ALL the conditions are met ('museum'
, 'cinema'
and 'car'
in the same string)
Why is the 'AND' statement acting like an 'OR'? How can we make this code more pythonic and more efficient?
We only check for 3 substrings here but it is more and more about thousands of occurrences we are looking for and it will be great to not repeat those conditions but have something more like an all()
or any()
statement that returns results and not a boolean.
&
instead ofand
?and
is logical operator which is meant to used in condition and&
is bit-wise operator which is meant to be used in expressions.