Skip to content

Instantly share code, notes, and snippets.

@atineoSE
Created December 28, 2020 19:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atineoSE/c498a7d7891a22208723acc482fa2ac4 to your computer and use it in GitHub Desktop.
Save atineoSE/c498a7d7891a22208723acc482fa2ac4 to your computer and use it in GitHub Desktop.
Simple python script to find a word in a list of sentences
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