0

I have a list and a string as follows.

mylist = ["tim tam", "yogurt", "strawberry"]
mystring = "I love to eat tim tam"

Now I want to check if mystring contains one or more words in mylist. If it contains the flag variable should be true, if not false.

My current code is as follows.

if mylist in mystring:
    flag = 1
else:
    flag = 0

However, I get the error TypeError: argument of type 'method' is not iterable. Not sure why it happens. Please help me.

  • This is inefficient, but `int(any(x in mystring for x in my list))` – cs95 Dec 19 '17 at 13:09
  • 4
    If you get that exception, then `mystring` is _not_ what you think it is, but a method. – tobias_k Dec 19 '17 at 13:10
  • See these answers: https://stackoverflow.com/questions/26577516/how-to-test-if-a-string-contains-one-of-the-substrings-in-a-list-in-pandas/60980656#60980656 – Grant Shannon Apr 01 '20 at 21:42

2 Answers2

4

You can use a generator expression in any to check each substring against your original string

>>> flag = any(i in mystring for i in mylist)
>>> flag
True

If you want an int instead of a bool you can modify the above to

>>> flag = int(any(i in mystring for i in mylist))
>>> flag
1
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
  • You can add that `all` and `any` are short circuiting in Python and this behavior is guaranteed. – godaygo Dec 19 '17 at 13:22
0

You can use str.contains- or lambda-function for filtering (I use it for DataFrame).

df=df.str.contains("keyword1|keyword2|keyword3", na=False)]

or

ddf[df.apply(lambda r: r.str.contains('keyword1|keyword2|keyword3', case=False).any(), axis=1)] 

Example:

mylist = 'tim tam|yogurt|strawberry'   
df = pd.DataFrame(df[df[[0,1,2,3...]].apply(lambda r: r.str.contains(mylist, case=False).any(), axis=1)])
TigerClaw
  • 167
  • 1
  • 2
  • 13