Python统计词频

 

import operator
text = '''
Youth means a temperamental predominance of courage over timidity, of the appetite for adventure over the love of ease. This often exits in a man of 60, more than a boy of 20.nobody grows merely by the number of years; we grow old by deserting our ideas. Years may wrinkle the skin, but to give up enthusiasm wrinkles the soul. Worry, fear, self-distrust1 bows the heart and turns the spirit back to dust
'''
# 先转换为小写
lo_text = txt.lower().split()
# 利用字典进行处理
dic = {}
for word in lo_text:
    if word not in dic:
        dic[word] = 1
    else:
        dic[word] = dic[word] + 1
#排序
swd = sorted(dic.items(),key = operator.itemgetter(1),reverse=True)
print(swd)

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用Python统计词频并绘制词云图,你可以按照以下步骤进行操作: 1. 首先,导入所需的库,包括pandas、WordCloud和matplotlib.pyplot。使用pandas库读取包含词频数据的Excel文件,并将数据转化为字典类型。 2. 创建一个WordCloud对象,并设置词云图的宽度、高度和背景颜色等参数。 3. 使用generate_from_frequencies方法生成词云图,传入之前转化的字典类型的词频数据。 4. 使用matplotlib.pyplot库展示生成的词云图。使用imshow函数显示词云图,并使用axis函数隐藏图形坐标轴。最后使用show函数显示图像。 下面是一个示例代码,展示了如何使用Python统计词频并绘制词云图: ```python import pandas as pd from wordcloud import WordCloud import matplotlib.pyplot as plt # 导入数据并转化为字典类型 df = pd.read_excel('C:/VV/学习/读研/毕业论文/毕业论文稿件1/中间稿/目的地形象&情感分析/情感分析.xlsx', sheet_name='重庆长尾词') word_freq = dict(zip(df\['词汇'\], df\['词频'\])) # 创建词云对象并生成词云 wc = WordCloud(width=800, height=400, background_color='white') wc.generate_from_frequencies(word_freq) # 使用matplotlib库展示生成的词云图 plt.imshow(wc, interpolation='bilinear') plt.axis('off') plt.show() ``` 这段代码会根据给定的Excel文件中的词频数据生成一个词云图,并在图像窗口中显示出来。你可以根据自己的需求修改文件路径和其他参数来适应你的数据和样式要求。 #### 引用[.reference_title] - *1* *2* *3* [Python通过已有词汇词频制作简单词云图](https://blog.csdn.net/Thourgh_07/article/details/129707534)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值