0

I have followed some examples on here for doing exactly what I am asking but I am not quite sure what I am doing wrong.

Basically, I want to find out if a given string contains any words from a list of words.

st = "hello this is a string"
ar = ['lo', 'str']

if any(e in st for e in ar):
  print(True)
else:
  print(False)

However I am getting True for this input. And similarly, when trying with some matching words I am getting False. Every source I have checked says to use the any in the way shown above to do what i desire. I just can't seem to get the hang of it. Any help would be appreciated!

1 Answers1

1

You can split the string into words and use list comprehension to check if there is at least one match

words=st.split(' ')
any([a==word for word in words for a in ar])
# False
fmarm
  • 4,209
  • 1
  • 17
  • 29