地址: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. 远程连接sql server 数据库的方法

    今天找了半天,终于解决了如何从本地连接到远程sql server服务器的方法. 1.首先确保打开远程服务器的sql server配置管理器,确保TCP/IP协议开启 2.WebConfig的连接字符格 ...

  2. Web负载均衡的几种方式

    Web负载均衡的几种实现方式 摘要:负载均衡(Load Balance)是集群技术(Cluster)的一种应用.负载均衡可以将工作任务分摊到多个处理单元,从而提高并发处理能力.目前最常见的负载均衡应用 ...

  3. java MYSQL做分页

    MySql中查询语句实现分页功能 语句: select * from 表名 where 条件 limit 要找第几页,每页多少行; import java.util.*; import java.sq ...

  4. delphi xe5 android 开发数据访问server端(一)

    第一篇我们破解并安装了xe5 第二篇我们搭建了开发环境 接下来我们开发一个三层的android程序 建立一个webservices  stand-alone vcl application 作为手机访 ...

  5. ubuntu maven 安装 设置

    http://blog.csdn.net/tiefanhe/article/details/9774189 1.安装 maven ,下载地址:http://maven.apache.org/downl ...

  6. GIT在LINUX下的基本操作

    没办法,看来,VIM技能也要同步练起来了. 离开了WIN的日常应用安乐窝,外面的世界有多精彩? GIT的错了我再改..呵呵 git clone http://username@1.2.3.4/repo ...

  7. 翻译文章“AST 模块:用 Python 修改 Python 代码”---!!注意ironpathyon未实现此功能

    https://github.com/upsuper/blog/commit/0214fdd084c4adf2de2ed9912d644fb59ce13a1c +Title: [翻译] AST 模块: ...

  8. Hibernate:1对1关系总结。

    QQ和QQ空间是1对1关系:配置如下 一.主键关联,双方共同维护表关系,以主键关联 public class QQ { private Long id; private QQZone zone; } ...

  9. 转:二十、java的抽象类

    http://blog.csdn.net/liujun13579/article/details/7737667 现实世界中,人们表征世界时,会把现实世界中的很多类具有相同特征的事物归为一个抽象类.比 ...

  10. 【HDOJ】3466 Proud Merchants

    先排序预处理,后01背包. #include <stdio.h> #include <string.h> #include <stdlib.h> #define M ...