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

2024-10-01 22:31:18 发布

您现在位置:Python中文网/ 问答频道 /正文

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')

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

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

我可能可以从那里找到剩下的


Tags: 方法textismydefcountassertfind
1条回答
网友
1楼 · 发布于 2024-10-01 22:31:18

我认为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'))

相关问题 更多 >

    热门问题