计算列表中单词的频率并按频率排序 [英] Count frequency of words in a list and sort by frequency

查看:90
本文介绍了计算列表中单词的频率并按频率排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Python 3.3

I am using Python 3.3

我需要创建两个列表,一个用于唯一词,另一个用于词频.

I need to create two lists, one for the unique words and the other for the frequencies of the word.

我必须根据频率列表对唯一单词列表进行排序,以使频率最高的单词在列表中排在首位.

I have to sort the unique word list based on the frequencies list so that the word with the highest frequency is first in the list.

我有文本设计,但是不确定如何在Python中实现它.

I have the design in text but am uncertain how to implement it in Python.

到目前为止,我发现的方法使用的是Counter或我们尚未学习的字典.我已经从包含所有单词的文件创建了列表,但是不知道如何找到列表中每个单词的频率.我知道我需要循环才能执行此操作,但无法弄清楚.

The methods I have found so far use either Counter or dictionaries which we have not learned. I have already created the list from the file containing all the words but do not know how to find the frequency of each word in the list. I know I will need a loop to do this but cannot figure it out.

这是基本设计:

 original list = ["the", "car",....]
 newlst = []
 frequency = []
 for word in the original list
       if word not in newlst:
           newlst.append(word)
           set frequency = 1
       else
           increase the frequency
 sort newlst based on frequency list 

推荐答案

使用此

from collections import Counter
list1=['apple','egg','apple','banana','egg','apple']
counts = Counter(list1)
print(counts)
# Counter({'apple': 3, 'egg': 2, 'banana': 1})

这篇关于计算列表中单词的频率并按频率排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆