程序
# -*- coding: utf-8 -*-
"""
功能:词频统计
作者:雾爱
日期:2021年12月5日
"""
text = 'I love python I love java I love python'
words = text.split(' ')
diff_words = list(set(words))
counts = []
for i in range(len(diff_words)):
counts.append(0)
for i in range(len(words)):
for j in range(len(diff_words)):
if diff_words[j] == words[i]:
counts[j] = counts[j] + 1
for word_count in zip(diff_words,counts):
print(word_count)
程序结果
可以根据自己的喜好更改text的内容