import nltk

 def freq_sorted(text,ranklimit):
fd=nltk.FreqDist(text)
cumulative = 0.0
for rank, (word,freq) in enumerate(sorted(fd.items(), key=lambda x: (-1*x[1], x[0]))[:ranklimit]):
cumulative += fd[word] * 100 / fd.N()
print "%3d %6.2f%% %s" % (rank+1, cumulative, word) def test_freq_sorted():
freq_sorted(nltk.corpus.brown.words(),15)

结果为:

  1   5.00% the
2 10.00% ,
3 14.00% .
4 17.00% of
5 19.00% and
6 21.00% to
7 22.00% a
8 23.00% in
9 23.00% that
10 23.00% is
11 23.00% was
12 23.00% for
13 23.00% ``
14 23.00% ''
15 23.00% The

Frequent Distribution sorted by frequency的更多相关文章

  1. [LeetCode] Top K Frequent Words 前K个高频词

    Given a non-empty list of words, return the k most frequent elements. Your answer should be sorted b ...

  2. Top K Frequent Words

    Given a non-empty list of words, return the k most frequent elements. Your answer should be sorted b ...

  3. [Swift]LeetCode692. 前K个高频单词 | Top K Frequent Words

    Given a non-empty list of words, return the k most frequent elements. Your answer should be sorted b ...

  4. LeetCode - Top K Frequent Words

    Given a non-empty list of words, return the k most frequent elements. Your answer should be sorted b ...

  5. [leetcode]692. Top K Frequent Words K个最常见单词

    Given a non-empty list of words, return the k most frequent elements. Your answer should be sorted b ...

  6. #Leetcode# 692. Top K Frequent Words

    https://leetcode.com/problems/top-k-frequent-words/ Given a non-empty list of words, return the k mo ...

  7. 692. Top K Frequent Words

    Given a non-empty list of words, return the k most frequent elements. Your answer should be sorted b ...

  8. [LC] 692. Top K Frequent Words

    Given a non-empty list of words, return the k most frequent elements. Your answer should be sorted b ...

  9. 【LeetCode】692. Top K Frequent Words 解题报告(Python)

    [LeetCode]692. Top K Frequent Words 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/top ...

随机推荐

  1. drupal错误: Maximum execution time of 240 seconds exceeded

    drupal7.5安装完成,导入汉化包时,出现错误: Fatal error: Maximum execution time of 240 seconds exceeded in D:\phpweb\ ...

  2. springMVC中ajax的运用于注意事项

    ajax的运用: 注意事项: dataType:"json"在ajax中可写可不写(ajax能够自动识别返回值类型),写了更加规范,可以在ajax识别错误返回值类型的时候,指定返回 ...

  3. awk 用法小结

    简介 awk是一个强大的文本分析工具,相对于grep的查找,sed的编辑,awk在其对数据分析并生成报告时,显得尤为强大.简单来说awk就是把文件逐行的读入,以空格为默认分隔符将每行切片,切开的部分再 ...

  4. UVALive 2323 Modular Multiplication of Polynomials(模拟)

    这是一个相对简单的模拟,因为运算规则已经告诉了我们,并且比较简单,不要被吓到…… 思路:多项式除以另外一个多项式,如果能除,那么他的最高次一定被降低了,如果最高次不能被降低,那说明已经无法被除,就是题 ...

  5. 11--tag 和transform属性

    tag 和transform属性 1.tag 标签,就相当于身份识别的标码,可以通过tag值获取对应的对象. 2.使用transform 实现对象的平移和旋转. // // ViewControlle ...

  6. mouseleave 和 mouseout 区别

    jQuery中的mouseleave和mouseout的区别 $("li").each(function(i) {            //删除的鼠标划过的显示与隐藏       ...

  7. HDU 2444 The Accomodation of Students

    首先是要构造二分图,然后二分图的最大匹配. 还有没完全证明过我的方法的正确性,但是AC了..... #include<cstdio> #include<cstring> #in ...

  8. linux下执行scrapy的爬虫定时任务

    刚开始执行scrapy crawl zentaos可以完成扫描 但是通过linux的crontab任务,只执行了连接mongodb的操作,并创建了索引 也就是说scrapy crawl zentaos ...

  9. zencart hosts本地解析

    C:\WINDOWS\system32\drivers\etc\hosts 127.0.0.1  www.aberc220.com   别人 192.168.1.64 www.aberc220.com ...

  10. [学习opencv]高斯、中值、均值、双边滤波

    http://www.cnblogs.com/tiandsp/archive/2013/04/20/3031862.html [学习opencv]高斯.中值.均值.双边滤波 四种经典滤波算法,在ope ...