<p>我正在使用Python3.3</p>
<p>我需要创建两个列表,一个用于唯一的单词,另一个用于单词的频率。</p>
<p>我必须根据频率列表对唯一的单词列表进行排序,以便频率最高的单词位于列表的第一位。</p>
<p>我有文本设计,但不确定如何在Python中实现它。</p>
<p>到目前为止,我发现的方法要么使用<code>Counter</code>,要么使用我们尚未学习过的词典。我已经从包含所有单词的文件中创建了列表,但不知道如何查找列表中每个单词的频率。我知道我需要一个循环来做这件事,但我想不出来。</p>
<p>基本设计如下:</p>
<pre><code> original list = ["the", "car",....]
newlst = []
frequency = []
for word in the original list
if word not in newlst:
newlst.<a href="https://www.cnpython.com/list/append" class="inner-link">append</a>(word)
set frequency = 1
else
increase the frequency
sort newlst based on frequency list
</code></pre>
<pre><code>words = file("test.txt", "r").read().split() #read the words into a list.
uniqWords = sorted(set(words)) #remove duplicate words and sort
for word in uniqWords:
print words.count(word), word
</code></pre>