我正在使用Python 3.3。
我需要创建两个列表,一个是单词的唯一列表,另一个是单词频率的列表。
我必须根据频率列表对唯一单词列表进行排序,以便具有最高频率的单词排在列表的第一位。
我已经有了文本中的设计,但不确定如何在Python中实现。
到目前为止,我发现的方法都使用了Counter
或字典,但我们还没有学习这些内容。我已经从包含所有单词的文件创建了列表,但不知道如何找到列表中每个单词的频率。我知道我需要循环来完成这个任务,但无法想出如何实现。
以下是基本设计:
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
set
吗?列表的count
方法呢?等等。请用有意义的术语描述问题。 - roippi