地址: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. C# WPF 从网络加载图片到byte[]数组中 Stream转byte[]代码

    折腾一中午 因为NetworkStream不支持Length属性 private byte[] GetImageFromResponse(WebResponse response) { using ( ...

  2. C语言实现的单链表

    链表是一种线性表,但是并不是顺序存储,而是每个节点里面存储着下一个节点的指针,把存储数据元素的数据串链起来. 单链表的基本实现: typedef int DataType;//定义单链表typedef ...

  3. zzuli oj 1165 实数的小数部分(指针专题)

    Description 读入一个实数,输出该实数的小数部分,小数部分若多余的末尾0,请去掉.如输入111111.12345678912345678900  则输出0.12345678912345678 ...

  4. caffe之(二)pooling层

    在caffe中,网络的结构由prototxt文件中给出,由一些列的Layer(层)组成,常用的层如:数据加载层.卷积操作层.pooling层.非线性变换层.内积运算层.归一化层.损失计算层等:本篇主要 ...

  5. Java多线程初学者指南(12):使用Synchronized块同步变量

    我们可以通过synchronized块来同步特定的静态或非静态方法.要想实现这种需求必须为这些特性的方法定义一个类变量,然后将这些方法的代码用synchronized块括起来,并将这个类变量作为参数传 ...

  6. hdu 3062

    2-SAT的入门题: 网上说这个算法最好的入门教材是:伍昱的<由对称性解2-SAT问题>的ppt和赵爽的论文<2-SAT 解法浅析>: 看了一下伍昱的ppt,很好理解! 而这道 ...

  7. 【HDU 2855】 Fibonacci Check-up (矩阵乘法)

    Fibonacci Check-up Problem Description Every ALPC has his own alpc-number just like alpc12, alpc55, ...

  8. RTSP

    相关博客: RTSP 很详细的英文文档 RTSP交互命令简介及过程参数描述   RTSP协议 http://blog.csdn.net/andyweike/article/details/621071 ...

  9. [转贴]C编译过程概述

    http://my.oschina.net/apeng/blog/105245 C 编译过程概述 目前Linux下最常用的C语言编译器是GCC(GNU Compiler Collection),它是G ...

  10. Java中int与Integer

    一般小写字母开头的是数据类型(如int double),大写字母开头的一般是封装为类(如Double),里面有很多方法,比如实行转换Integer.parseInt(arg0),可以把其他类型的数据转 ...