Python script to find a words | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Python script to find a words

There is a python script that loads cvs file with some part of text in every cell in the column A. I want to modify the script to get information how many times the phrase 'green book' appears in every cell. When I run this code it count only one mention this phrase in cell but there some of the cell contain at least 2 mentions What do I do wrong? df_['CheckWords'] = df_['transcript'].map(check_words) def check_words(text): i = 0 if len(i in text for i == 'green book'): i += 1 return (i)

14th Jul 2020, 3:38 PM
Zukovski Zuk
Zukovski Zuk - avatar
2 Answers
+ 3
May be i understand it wrong, so please correct me. I feel that some more information is needed from your side. I assume, that you want to find the number of occurrences of a substring in a string. s = 'each time i saw a green book, it looks like "GREEN Book"' sub = 'green book' count = s.lower().count(sub.lower()) print(count) # result is 2
14th Jul 2020, 6:27 PM
Lothar
Lothar - avatar
0
Your code can never increase i higher than 1. But I would like to add, that you could compare the length directly, if you first replace all occurrences and divide the difference by the length of the search string.
14th Jul 2020, 4:25 PM
Sandra Meyer
Sandra Meyer - avatar