查找多个句子中的出现
finding occurrences in Multiple sentences
我是 python 的新手,但是在搜索互联网并回顾我的研究后,我似乎无法找到如何在多个句子中找到重复的单词。我的目标是定义单词 python 在这些字符串中出现的次数。我尝试了 split() 方法和 count.(python) 甚至尝试制作字典和 word_counter 最初我被教导做基础知识的一部分但是我的研究中没有任何东西之前给我看了类似的东西。我需要能够显示单词的频率。 python 出现了 4 次。任何帮助将不胜感激
python_occurs = ["欢迎来到我们的 Python 计划", "Python 是我最喜欢的语言!", "我害怕 Pythons", “我爱 Python”]
一种直接的方法是使用 split
遍历每个单词。对于每个单词,它被转换为小写,并且使用 count
.
计算其中 "python"
出现的次数
我想您的方法不起作用的原因可能是您忘记将字母更改为小写。
python_occurs = ["welcome to our Python program", "Python is my favorite language!", "I am afraid of Pythons", "I love Python"]
count = 0
for sentence in python_occurs:
for word in sentence.split():
# lower is necessary because we want to be case-insensitive
count += word.lower().count("python")
我是 python 的新手,但是在搜索互联网并回顾我的研究后,我似乎无法找到如何在多个句子中找到重复的单词。我的目标是定义单词 python 在这些字符串中出现的次数。我尝试了 split() 方法和 count.(python) 甚至尝试制作字典和 word_counter 最初我被教导做基础知识的一部分但是我的研究中没有任何东西之前给我看了类似的东西。我需要能够显示单词的频率。 python 出现了 4 次。任何帮助将不胜感激
python_occurs = ["欢迎来到我们的 Python 计划", "Python 是我最喜欢的语言!", "我害怕 Pythons", “我爱 Python”]
一种直接的方法是使用 split
遍历每个单词。对于每个单词,它被转换为小写,并且使用 count
.
"python"
出现的次数
我想您的方法不起作用的原因可能是您忘记将字母更改为小写。
python_occurs = ["welcome to our Python program", "Python is my favorite language!", "I am afraid of Pythons", "I love Python"]
count = 0
for sentence in python_occurs:
for word in sentence.split():
# lower is necessary because we want to be case-insensitive
count += word.lower().count("python")