I want to print 1 if the word is in the paragraph, if not then print 0
The first line contains the word bestselling yet lambda is printing 0
A good way to do that is to use any function and cast to int the result.
text = "this is a text used for an example."
first_list = ["word", "second_word", "example"]
second_list = ["word", "second_word", "third_word"]
is_in = int(any(k in text for k in first_list))
print(is_in) # print 1
not_in = int(any(k in text for k in second_list))
print(not_in) # print 0
A way to search in a DataFrame would be using the contains
method of str (documentation here) . In your case you want to search whether multiple words are in the text, so a regular expression could be used:
df["sun"][0].str.contains("brilliant|bestselling|best|best-selling|loved|great|amazing", regex=True)
If you also want to match the word regardless of if it's in lowercase or uppercase you can add:
import re
df["sun"][0].str.contains("brilliant|bestselling|best|best-selling|loved|great|amazing", flags=re.IGNORECASE, regex=True)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.