if "test" in ['testtext', 'aaaa', 'texttext']:
print("yes")
else:
print("no")
This will always output "no". How can I change this so it outputs "yes" as the word "test" is present in 'testtext' even though it's just a portion of it?
You can use any() builtin function:
if any("test" in w for w in ['testtext', 'aaaa', 'texttext']):
print("yes")
else:
print("no")
Prints:
yes
you can also try with operator builtin module with operator.contains
txt = ['testtext', 'aaaa', 'texttext']
[operator.contains(i,'test') for i in txt]
output:
[True, False, False]
if u want get word which is true. first convert ur txt file in numpy array then then try this.
txt = np.array(txt)
x = [operator.contains(i,'test') for i in txt]
txt[x]
output:
array(['testtext'], dtype='<U8')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With