python - How to recode line so an exact sentence must be in the list for it to match -


x = ["cookie flavored water yummy 6", "coding complicated 16", "help 7"]  in x:     if "flavored" in x:         print ("yes")     else:         print ("no") 

i want exact string "cookie flavored water yummy" in list acceptable don't want 6 part included. i'm befuddled on how accomplish this. objective might change first element different element.

you're iterating on x i check if string belongs list, not element, false.

to check if element of x contains "cookie flavored water yummy"

x = ["cookie flavored water yummy 6", "coding complicated 16", "help 7"]  in x:     print ("yes" if "cookie flavored water yummy" in else "no") 

on other hand, exact string match use in without loop, loop being made on x in operator:

print ("yes" if "cookie flavored water yummy" in x else "no") 

if need exact string match on great number of elements, consider putting elements in set instead because lookup time smaller (hashing involved). code remains same apart that.


Comments

Popular posts from this blog

serialization - Convert Any type in scala to Array[Byte] and back -

matplotlib support failed in PyCharm on OSX -

python - Matplotlib: TypeError: 'AxesSubplot' object is not callable -