地址: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. Eclipse中的add jars和add external jars有什么区别(转载)

    转载自:http://blog.csdn.net/yasi_xi/article/details/12772457 add jars和add external jars有什么区别?   add ext ...

  2. iOS开发 - NSBundle, NSDevice, NSLocale

    iOS的APP的应用开发的过程中,有时为了bug跟踪或者获取用反馈的需要自动收集用户设备.系统信息.应用信息等等,这些信息方便开发者诊断问题,当然这些信息是用户的非隐私信息,是通过开发api可以获取到 ...

  3. javascript之事件

    客户端javascript程序采用了异步事件驱动编程模型. 相关事件的几个概念: 事件类型(event type):用来说明发生什么类型事件的字符串: 事件目标(event target):发生事件的 ...

  4. centos6.5搭建vpn服务器

    1 安装ppp yum install -y ppp  2 安装pptpd yum install pptpd   //  rpm -Uvh http://poptop.sourceforge.net ...

  5. cisco上的RIP V2加上MD5认证配置测试成功

    R1: Router#show run Building configuration... Current configuration : bytes ! version 12.3 service t ...

  6. 李洪强iOS开发Swift篇—06_流程控制

    李洪强iOS开发Swift篇—06_流程控制 一.swift中的流程控制 Swift支持的流程结构如下: 循环结构:for.for-in.while.do-while 选择结构:if.switch 注 ...

  7. [wikioi]过河卒

    棋盘型动态规划.(PPT:http://wenku.baidu.com/view/56badad850e2524de5187ea3.html)该类动态规划有一个共性,那就是在一个矩阵中(一般是二维矩阵 ...

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

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

  9. C++开发与Windows API

    Windows API 向 C++ 开发人员提出了一项挑战. 组成 API 的众多库大都表现为 C 语言风格的函数和句柄或是 COM 风格的接口. 这些用起来都不太方便,需要进行一定的封装或间接操作. ...

  10. leetcode面试准备: Substring with Concatenation of All Words

    leetcode面试准备: Substring with Concatenation of All Words 1 题目 You are given a string, s, and a list o ...