python字母频率_科学网-Python统计字母频数和频率-吕波的博文
方案一 统计字符串中的字母频数
import collections
import re
d = collections.defaultdict(int)
S = "testTypecopyri4g3HACBCtcFor the ginpraise.ll be dog ssditsl-+-___*&^%icense()ation."
#convert to lower case
s = S.lower()
#match non-letter chars
rule = re.compile(r"[^a-z]")
#delete non-letter chars
s_result = rule.sub("", s)
#calculate the couter
for c in s_result:
d[c] += 1
print d
#rank according to the letter
print sorted(d.iteritems(), key = lambda d:d[0])
#rank according to the counter
print sorted(d.iteritems(), key = lambda d:d[1])
方案二 统计字符串中的字母频率
import collections
import re
sum = 0
d = collections.defaultdict(float)
S = "testTypecopyri4g3HACBCtcFor the ginpraise.ll be dog ssditsl-+-___*&^%icense()ation."
#convert to lower case
s = S.lower()
#match non-letter chars
rule = re.compile(r"[^a-z]")
#delete non-letter chars
s_result = rule.sub("", s)
#calculate the couter
for c in s_result:
d[c] += 1
#calculate the total number of letters
sum += 1
print "The total number of letters is: %d"%sum
#calculate the frequency
for c in d:
d[c] = d[c]/sum
d[c] = round(d[c], 10)
print d
#rank according to the letter
print sorted(d.iteritems(), key = lambda d:d[0])
print sorted(d.iteritems(), key = lambda d:d[0], reverse = True)
#rank according to the counter
print sorted(d.iteritems(), key = lambda d:d[1])
print sorted(d.iteritems(), key = lambda d:d[1], reverse = True)
方案三 统计文本文件中的字母频数
import collections
import re
d = collections.defaultdict(int)
#open the txt file
myfile = open('test.txt')
for line in myfile:
#delete the "n" at the end of the line
line = line.rstrip('n')
#convert to lower case
line = line.lower()
#match non-letter chars
rule = re.compile(r"[^a-z]")
#delete non-letter chars
line_result = rule.sub("", line)
#calculate the couter
for c in line_result:
d[c] += 1
myfile.close()
print d
#rank according to the letter
print sorted(d.iteritems(), key = lambda d:d[0])
#rank according to the counter
print sorted(d.iteritems(), key = lambda d:d[1])
方案四 统计文本文件中的字母频率
import collections
import re
sum = 0
d = collections.defaultdict(float)
#open the txt file
myfile = open('test.txt')
for line in myfile:
#delete the "n" at the end of the line
line = line.rstrip('n')
#convert to lower case
line = line.lower()
#match non-letter chars
rule = re.compile(r"[^a-z]")
#delete non-letter chars
line_result = rule.sub("", line)
#calculate the couter
for letter_x in line_result:
d[letter_x] += 1
sum += 1
myfile.close()
#calculate the total number of letters
print "The total number of letters is: %d"%sum
#calculate the frequency
for c in d:
d[c] = d[c]/sum
d[c] = round(d[c], 10)
print d
#rank according to the letter
print sorted(d.iteritems(), key = lambda d:d[0])
print sorted(d.iteritems(), key = lambda d:d[0], reverse = True)
#rank according to the counter
print sorted(d.iteritems(), key = lambda d:d[1])
print sorted(d.iteritems(), key = lambda d:d[1], reverse = True)
转载本文请联系原作者获取授权,同时请注明本文来自吕波科学网博客。
收藏
分享
分享到:
python字母频率_科学网-Python统计字母频数和频率-吕波的博文相关推荐
- python 面板数据分析_科学网—Python中的结构化数据分析利器-Pandas简介 - 郑俊娟的博文...
此文转载于XXXXXX处... Pandas是python的一个数据分析包,最初由AQR Capital Management于2008年4月开发,并于2009年底开源出来,目前由专注于Python数 ...
- python提取句子_科学网—Python提取句子 - 吕波的博文
将一段话中的句子分离出来不是一件容易的事.因为句子的开头和结尾并不是很规则,而且句子内部会出现句号.这使得通过单一的正则表达式分离句子是不可能的.有时你能成功,但大多数时候你会出错.这里我们用nltk ...
- python读取网站_科学网—python 获取网址 - 林清莹的博文
Python获取网址的内容# coding=utf-8 import urllib url = "http://www.baidu.com" data = urllib.urlop ...
- python 数据去重_科学网—python学习——根据条件提取数据,并去重 - 李立的博文...
[Python字符串提取] 摘要:根据要求进行字符串的提取,并去重 导入分析所需的库import pandas as pd 构造数据集 as1 = pd.DataFrame({'a':[1,2,3,4 ...
- python编程口诀_科学网—Python编程技巧汇总 - 高关胤的博文
正在学习python编程,把一些小技巧记录下来备查 ======================计算技巧========================== 正常的条件语句如下if a>b:c= ...
- python离散积分_科学网—python数据处理笔记(三)通道积分图 - 钱磊的博文
通道积分是除了p-v图外又一件分析分子云气体运动的利器.在不同的通道查看,可以看到一些速度不同寻常的气体,这些气体就是恒星形成研究中十分感兴趣的高速气体.这些高速气体对于分子云中的能量平衡以及恒星的形 ...
- python笔记图片_科学网—python数据处理笔记(二)p-v图 - 钱磊的博文
python数据处理笔记(二)p-v图 已有 9903 次阅读 2012-5-24 17:46 |个人分类:知识|系统分类:科研笔记| Python, 分子云, 常用工具, p-v图 p-v图是分析分 ...
- python画箭头_科学网-Python matplotlib quiver—画箭头、风场、量场图-张伟的博文
用像素点坐标画图 箭头关键的一个参数是长度,长度可以通过参数scale来设置,如果你多次使用quiver(),只要保证参数scale一致,那么箭头长度就会与风速 的值成正比,可按照下面我贴出的代码那样 ...
- python频次统计图_科学网—Python小例:统计文本中单词出现的频次 - 康建的博文...
import re zen=''' The Zen of Python, by Tim Peters Beautiful is better than ugly. Explicit is better ...
最新文章
- Python学习笔记:SMTP服务器
- 清华大学计算机系毕业季博论 | 预荐未来的自己
- bzoj 3144: [Hnoi2013]切糕
- Canvas事件绑定
- php图形验证码接口,php生成图形验证码
- 记一次 React 组件无法更新状态值的问题分析与解决
- 人工智能可控核聚变量子计算机,中国终于实现了可控核聚变
- 宇视摄像头ip搜索软件下载_搜索资源?下载资源?一款软件就给你安排的妥妥的...
- angular动态绑定样式以及改变UI框架样式的方法
- python 装饰器(复杂一点的)
- python2和python3同时安装在Windows上
- 我用 Python 抓取了 7000 多本电子书
- 《Spring Boot极简教程》第9章 Spring Boot集成Scala混合Java开发
- matlab fseek ftell,fseek函数、ftell函数和fflush函数
- 高斯光束matlab 仿真,高斯光束的matlab仿真
- mysql webmail ubuntu12.04 imap_如何在Ubuntu 18.04 LTS上安装最新的Roundcube Webmail
- python怎么提取文件内容_python怎么提取出文件里的指定内容
- python里的百分号_python里百分号什么意思
- 基于中移物联网MQTT协议的ESP8266远程开机实例
- jsencrypt加密,并解决Message too long for RSA