地址:http://www.codewars.com/kata/53e895e28f9e66a56900011a/train/python

Write a function that takes a piece of text in the form of a string and returns the letter frequency count for the text. This count excludes numbers, spaces and all punctuation marks. Upper and lower case versions of a character are equivalent and the result should all be in lowercase.

The function should return a list of tuples sorted by the most frequent letters first. Letters with the same frequency are ordered alphabetically. 
For example:
 letter_frequency('aaAabb dddDD hhcc')
will return
 [('d',5), ('a',4), ('b',2), ('c',2), ('h',2)]

Letter frequency analysis is often used to analyse simple substitution cipher texts like those created by the Caesar cipher.

代码,注释比较详细:

def letter_frequency(text):
ans = []
dic = {}
#长度计算放在循环里效率低
lenOfText = len(text) for i in range(0,lenOfText):
#提前处理成小写
alp = text.lower()[i] #非字母不统计
if alp.isalpha() == False:
continue #用字典统计字母个数
if dic.has_key(alp):
dic[alp] += 1
else:
dic[alp] = 1 #反转字典元素存入list
for k,v in dic.items():
ans.append((v,k))
#按出现频率由高到底排序
ans.sort(reverse=True) #频次相同,按字母序
lenOfAns = len(ans)
for i in range(0,lenOfAns-1):
for j in range(i+1,lenOfAns):
if ans[i][:1] == ans[j][:1] and ans[i][-1:] > ans[j][-1:]:
tmp = ans[i]
ans[i] = ans[j]
ans[j] = tmp
#交换字母和频次位置
nans = []
for i in range(0,lenOfAns):
nans.append((ans[i][1],ans[i][0])) return nans

  

Character frequency的更多相关文章

  1. How to calculate bits per character of a string? (bpc) to read

      http://stackoverflow.com/questions/17797922/how-to-calculate-bits-per-character-of-a-string-bpc up ...

  2. huffman编码——原理与实现

    哈夫曼算法原理 Wikipedia上面说的非常清楚了,这里我就不再赘述,直接贴过来了. 1952年, David A. Huffman提出了一个不同的算法,这个算法能够为不论什么的可能性提供出一个理想 ...

  3. DNS Tunnel隧道隐蔽通信实验 && 尝试复现特征向量化思维方式检测

    1. DNS隧道简介 DNS隧道技术是指利用 DNS协议建立隐蔽信 道,实现隐蔽数据传输.最早是在2004年 DanKaminsky 在 Defcon大会上发布的基于 NSTX 的 DNS隐蔽 隧道工 ...

  4. 【转】常用算法复习及实现(C++版)

    一.霍夫曼树实现 给定n个权值作为n个叶子结点,构造一棵二叉树,若带权路径长度达到最小,称这样的二叉树为最优二叉树,也称为哈夫曼树(Huffman tree).哈夫曼树是带权路径长度最短的树,权值较大 ...

  5. UTF-8, UTF-16, UTF-32 & BOM

    FAQ - UTF-8, UTF-16, UTF-32 & BOM http://www.unicode.org/faq/utf_bom.html General questions, rel ...

  6. DNS通道检测 国外学术界研究情况——研究方法:基于流量,使用机器学习分类算法居多,也有使用聚类算法的;此外使用域名zif low也有

    http://www.ijrter.com/papers/volume-2/issue-4/dns-tunneling-detection.pdf <DNS Tunneling Detectio ...

  7. DNS Tunneling及相关实现——总之,你发起攻击都需要一个DNS server,下载一些工具作为client发起数据,server收集数据并响应

    摘自:http://www.freebuf.com/sectool/112076.html DNS Tunneling,是隐蔽信道的一种,通过将其他协议封装在DNS协议中传输建立通信.因为在我们的网络 ...

  8. DNS隧道和工具

    DNS Tunneling及相关实现 转自:http://www.freebuf.com/sectool/112076.html DNS Tunneling,是隐蔽信道的一种,通过将其他协议封装在DN ...

  9. UVA 10789 题解

    Prime Frequency Given a string containing only alpha-numerals (0-9,A-Z and a-z) you have to count th ...

随机推荐

  1. web design tools

    https://www.google.com/webdesigner/ http://html.adobe.com/edge/inspect/ http://www.creativebloq.com/ ...

  2. 用Cornerstone配置SVN

    iOS 用CornerStone配置SVN,HTTP及svn简单使用说明 分类: iOS / OC2014-11-11 11:19 3149人阅读 评论(0) 收藏 举报   目录(?)[+]   转 ...

  3. c/c++多级指针

    c/c++多级指针 如图: # include <stdio.h> int main(void) { ; int * p = &i; //p只能存放int类型变量的地址 int * ...

  4. JVM 学习笔记(二)

    JVM 堆中几乎存放着java中所有的对象实例,在在垃圾回收前先要判断对象是否已死,这里对对象的判断主要有: 1.  引用计数法 给对象中添加一个引用计数器,每当有一个地方引用他时,计数器就加1:当引 ...

  5. BZOJ 1717: [Usaco2006 Dec]Milk Patterns 产奶的模式

    Description 农夫John发现他的奶牛产奶的质量一直在变动.经过细致的调查,他发现:虽然他不能预见明天产奶的质量,但连续的若干天的质量有很多重叠.我们称之为一个"模式". ...

  6. vs2010自带的报表

    本例用来显示Northwind中的order details表中的数据交分组 1.建立一WinForm程序,并建立一数据库连接,选择order details表,此时会自动建立一个xsd的数据集类,如 ...

  7. warning: push.default is unset;

    git push warning questions This warning was introduced in Git 1.7.11 along with the simple style of ...

  8. C# 利用BarcodeLib.dll生成条形码

    首先效果: 1:首先下载BarcodeLib.dll 下载地址 http://pan.baidu.com/share/link?shareid=2590968386&uk=2148890391 ...

  9. struts2错误:The Struts dispatcher cannot be found.

    struts2错误:The Struts dispatcher cannot be found. The Struts dispatcher cannot be found. This is usua ...

  10. linux下建立无线wifi------简单实用!

    一 安装必要软件安装hostapd :    sudo apt-get install hostapd安装DHCP:    sudo apt-get install dhcp3-server 二 配置 ...