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.

3
  • This is inefficient, but int(any(x in mystring for x in my list)) Commented Dec 19, 2017 at 13:09
  • 4
    If you get that exception, then mystring is not what you think it is, but a method. Commented Dec 19, 2017 at 13:10
  • See these answers: stackoverflow.com/questions/26577516/… Commented Apr 1, 2020 at 21:42

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

1 Comment

You can add that all and any are short circuiting in Python and this behavior is guaranteed.
1

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)])

1 Comment

Huh... where has pandas come from?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.