0
str1 = 'Thank you very much. It was really great help for me'
str2 = 'Yes, I am'
str3 = 'No, It wasn`t'
arr = ['it', 'no', 'jack']

if str1 in arr => true
if str2 in arr => false
if str3 in arr => true

i don't care upper or lower.

i want to check that i saved array and check string value if there is a word.
how can i check it?

i tried

print(arr in str)

but error occored 'in ' requires string as left operand, not list

somebody help me

5 Answers5

2

If I understand correctly what is your desired test here:

print(any(s.lower() in str1.lower() for s in arr))
print(any(s.lower() in str2.lower() for s in arr))
print(any(s.lower() in str3.lower() for s in arr))
Or Y
  • 2,088
  • 3
  • 16
0

As the error suggests, it is the other way around:

print(str1 in arr)
print(str2 in arr)
print(str3 in arr)

Consider looking at the traceback and try to make more sense out of it. Most issues can be debugged quite easily from the traceback and googling on the specific issue.

However, looks like you are trying to check any word inside arr lies in your particular string str, you can do this:

print(any(s in str1 for s in arr))
Jarvis
  • 8,494
  • 3
  • 27
  • 58
0

Try any method :)

print(any(i in arr for i in str1.lower())
print(any(i in arr for i in str2.lower())
print(any(i in arr for i in str3.lower())
Nanthakumar J J
  • 860
  • 1
  • 7
  • 22
0

Since you're apparently only trying to match words but not punctuations, it's easiest to use a regex pattern with desired words joined as an alternation pattern, enclosed with word boundary checks on both ends:

import re
re.search(rf"\b({'|'.join(arr)})\b", str1, re.I)

This returns a truthy value for str1 and str3, and a falsey value for str2, from your sample inputs.

blhsing
  • 91,368
  • 6
  • 71
  • 106
0

Just do this if you don't care about upper or lower:

print(str1 in arr, str2 in arr, str3 in arr)
Rakshit Ajay
  • 7
  • 1
  • 8