Created
December 28, 2020 19:36
-
-
Save atineoSE/c498a7d7891a22208723acc482fa2ac4 to your computer and use it in GitHub Desktop.
Simple python script to find a word in a list of sentences
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def find_occurrences(phrasesList, text): | |
total_count = 0 | |
for phrase in phrasesList: | |
local_count = 0 | |
words = phrase.split() | |
for word in words: | |
if text in word: | |
local_count += 1 | |
#print("Found {} ocurrences of \"{}\" in \"{}\"".format(local_count, text, phrase)) | |
total_count += local_count | |
#print("Found {} total ocurrences so far".format(total_count)) | |
return total_count | |
assert find_occurrences(["welcome to our Python program", "Python is my favorite language!", "I am afraid of Python", "I love Python"], "Python") == 4 | |
assert find_occurrences(["this is the best day", "Python is the best language for learning programming", "I am learning", "I love learning"], "learning") == 3 | |
assert find_occurrences(["welcome", "language", "I am", "I love"], "Python") == 0 | |
assert find_occurrences(["What are you doing?", "you like programming?", "We are students", "We are learners"], "are") == 3 | |
assert find_occurrences(["welcome welcome", "wikipedia", "wonderland", "we"], "w") == 5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment