def find_occurrences(text, itemsList):
count = dict()
words = text.split()
return count;
assert find_occurrences(['welcome to our Python program', 'Python is my favourite language!', 'I love Python'], 'Python')
assert find_occurrences(['this is the best day', 'my best friend is my dog'], 'best')
我必须编写代码来帮助我计算一个单词在句子列表中的出现次数。
我正在尝试拆分文本,但它不允许我这样做。我想我需要找到一种方法来阅读句子,然后再拆分,但我想不出这样做的方法。如果有人能帮助我或为我指明正确的方向,那将是有帮助的。
我可能能从那里弄清楚剩下的。
我认为string.count()
应该这样做。只需迭代输入列表:
def find_occurrences(text, itemsList):
occurs = 0
for i in text:
occurs += i.count(itemsList)
return occurs
print(find_occurrences(['welcome to our Python program', 'Python is my favourite language!', 'I love Python'], 'Python'))
print(find_occurrences(['this is the best day', 'my best friend is my dog'], 'best'))