如何在python中拆分文本统计字符串列表中出现的次数

如何在python中拆分文本统计字符串列表中出现的次数,python,list,assert,Python,List,Assert,我必须编写代码来帮助我计算一个单词在句子列表中出现的次数 我正试图拆分文本,但它不允许我拆分。我想我需要找到一种方法来读这个句子,然后把它分开,但我想不出一种方法来这样做。如果有人能帮我或给我指出正确的方向,那将是很有帮助的 我可能可以从中找出其余的部分。我认为string.count()应该在这里完成。只需遍历输入列表: def find_occurrences(text, itemsList): count = dict() words = text.split() retu

我必须编写代码来帮助我计算一个单词在句子列表中出现的次数

我正试图拆分文本,但它不允许我拆分。我想我需要找到一种方法来读这个句子,然后把它分开,但我想不出一种方法来这样做。如果有人能帮我或给我指出正确的方向,那将是很有帮助的

我可能可以从中找出其余的部分。

我认为string.count()应该在这里完成。只需遍历输入列表:

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):
    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')
这是因为函数调用中的“text”参数是列表而不是字符串。“split”是strings的一种方法,这是因为函数调用中的“text”参数是列表而不是字符串。“拆分”是字符串的一种方法