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. 在DLL中导出另一静态库中的函数

    开发环境: win7_x64.VS2013 应用场景: 动态库A依赖动态库B,而动态库B又使用了静态库C:有些情况下,我们需要将C从B里面导出,然后提供给A使用. 正文: Step1: 1.新建测试静 ...

  2. HDU 2579/BFS/ Dating with girls(2)

    题目链接 /* 题意是是传统的迷宫加上一个条件,墙壁在k的整倍数时刻会消失,那么求到达出口的最短时间. 关键点在于某个点最多被走k次,标记vis[x][y][time%k]即可. */ #includ ...

  3. 单元测试、自动化测试、接口测试过程中的Excel数据驱动(java实现)

    import java.io.FileInputStream;import java.io.InputStream;import java.util.HashMap;import java.util. ...

  4. CodeForces 701C They Are Everywhere(map的应用)

    这个题比较好的解决办法,我觉得还是map,map的size可以很快的知道我们选了几个字母,而且可以作为计数器,知道每一个字母出现了多少次, erase函数可以清除掉一个元素. 所以,定义两个指针L和R ...

  5. cocos2d-x 3.x 橡皮擦功能

    1.HelloWorldScene.h cocos2d::DrawNode* _eraser; cocos2d::RenderTexture*_renderTexture; 2.HelloWorldS ...

  6. cocos2d-x 3.x 触摸事件

    HelloWorldScene.h bool touchBegan(cocos2d::Touch*touch, cocos2d::Event*event);//触摸开始,返回bool类型 void t ...

  7. php简单命令代码集锦

    if(file_exists("file.htm"))// 检查是否存在此文件 if(file_exists("chat"))//检查是否存在此文件夹 rena ...

  8. 10、桥接模式(Bridge)

    桥接模式就是把事物和其具体实现分开,使他们可以各自独立的变化.桥接的用意是:将抽象化与实现化解耦,使得二者可以独立变化,像我们常用的JDBC桥DriverManager一样,JDBC进行连接数据库的时 ...

  9. 页面加载完毕执行多个JS函数

    通常我们需要在打开页面时加载脚本,这些脚本必须在页面加载完毕后才可以执行,因为这时候DOM才完整,可以利用window.onload确保这一点,如:window.onload=firstFunctio ...

  10. UIApplication 用法解析

    UIApplication,代表的是整个应用做的事,因此每个程序只能有一个,系统使用的是单例模式,就是上面的[UIApplication sharedApplication]来得到一个实例.这个单例实 ...