1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > python统计字符串中每个单词出现的次数_python中每个单词出现的次数是一组字符串?...

python统计字符串中每个单词出现的次数_python中每个单词出现的次数是一组字符串?...

时间:2021-01-03 12:21:15

相关推荐

python统计字符串中每个单词出现的次数_python中每个单词出现的次数是一组字符串?...

你可以这样得到你的分数:import re

sentence = list()

sentence.append(" hello my name is John")

sentence.append("good morning I am John")

sentence.append("hello I am Smith")

value = dict()

for sentenceC in sentence:

for word in sentenceC.strip().split(" "): # split with whitespace

try:

value[word.lower()] += 1

except KeyError:

value[word.lower()] = 1

print (value)

score = dict()

number = 1

for sentenceC in sentence:

for word in sentenceC.strip().split(" "): # split with whitespace

try:

score[number] += value[word.lower()]

except KeyError:

score[number] = value[word.lower()]

number += 1

print score

#output: {1: 7, 2: 8, 3: 7}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。