如何计算最少使用的单词

时间:2016-11-05 10:19:14

标签: python frequency

wList是一个包含文本中[(单词,单词的频率)]的列表。 我想知道单词的使用频率最低。 但是,当我运行以下代码时,我得到了

IndexError: list index out of range 

请告诉我我做错了什么

def countLeastWds(wList) :
    cnt = 0
    leastUsed = 1
    leastUsed = wList[(-1)]
    wList.reverse()
    for fq in wList :
        if fq != leastUsed :
        break
        else : cnt = cnt + 1
return leastUsed, cnt

1 个答案:

答案 0 :(得分:0)

import collections

d=dict([(2,'hello'),(3,'bye'),(1,'there')])
print collections.OrderedDict(sorted(d.items())).popitem(last=False)

Output: (1, 'there')