[英]How to split text in python count number of occurrences in a list of strings
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')
我必須編寫代碼來幫助我計算一個單詞在句子列表中出現的次數。
我正在嘗試拆分文本,但它不允許我這樣做。 我想我需要找到一種方法來閱讀句子然后將其拆分,但我想不出這樣做的方法。 如果有人可以幫助或指出正確的方向,那將很有幫助。
我可能可以從那里算出 rest。
我認為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'))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.