利用小程序统计词频

程序

# -*- 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的内容

猜你喜欢

转载自blog.csdn.net/qq_62491692/article/details/121734312