python统计字符串里每个字符的次数
方法一:
推导式
dd="ewq4aewtaSDDSFDTFDSWQrtewtyufashas"
print {i:dd.count(i) for i in dd}

方法二:
counter
import collections
dd="ewq4aewtaSDDSFDTFDSWQrtewtyufashas"
obj = collections.Counter(dd)
print obj

取值:
for k,v in obj.items():
print (k,v)

方法三:
和方法一类似
dd="ewq4aewtaSDDSFDTFDSWQrtewtyufashas"
for i in set(dd):
print(i,dd.count(i))

方法四:
dd = 'ewq4aewtaSDDSFDTFDSWQrtewtyufashas'
d = {}
for c in dd:
d[c] = (d[c] + 1) if (c in d) else (1)
print d

打印出排行前三的字符
dd="ewq4aewtaSDDSFDTFDSWQrtewtyufashas"
obj = collections.Counter(dd)
print obj.most_common(3)

python统计一个文档中 各个字符出现的次数
f = file("data.txt")
s = f.read()
# 这里的s采用文件的方式读取
global list_all
global list_to_statistic
def tran_s_to_list(s):
list_all = []
l = len(s)
# 得到长度,遍历
for x in xrange(0,l):
# 当x不在list中,即第一次出现,追加到list中
if not s[x] in list_all:
list_all.append(s[x])
return list_all
def statistic(s, list_all, list_to_statistic):
l = len(s)
for x in xrange(0,l):
遍历字符串,找到每一个char在list中的index,在list_statistic相应位置加一
list_to_statistic[list_all.index(s[x])] = list_to_statistic[list_all.index(s[x])]+1
# print list_all.index(s[x]),
# print
list_all = tran_s_to_list(s)
# 复制一个和list等长的数组list_statistic,并且全部赋值为0
list_to_statistic = list_all[ : ]
for x in xrange( 0, len(list_all) ):
list_to_statistic[x] = 0
statistic(s, list_all, list_to_statistic);
# 打印
listlength = len(list_all)
for x in xrange(0, listlength):
print str(list_all[x])+"" + "---appers---"+str(list_to_statistic[x])+"---times"
python统计字符串里每个字符的次数的更多相关文章
- python 统计字符串中指定字符出现次数的方法
python 统计字符串中指定字符出现次数的方法: strs = "They look good and stick good!" count_set = ['look','goo ...
- 统计字符串中每个字符出现的次数(Python)
#统计字符串中每个字符出现的次数 以The quick brown fox jumps over the lazy dog为例 message='The quick brown fox jumps o ...
- 使用Map,统计字符串中每个字符出现的次数
package seday13; import java.util.HashMap; import java.util.Map; /** * @author xingsir * 统计字符串中每个字符出 ...
- Java基础知识强化之集合框架笔记61:Map集合之统计字符串中每个字符出现的次数的案例
1. 首先我们看看统计字符串中每个字符出现的次数的案例图解: 2. 代码实现: (1)需求 :"aababcabcdabcde",获取字符串中每一个字母出现的次数要求结果:a(5) ...
- PHP统计字符串里单词查询关键字
<?function full_count_words($str) { //返回完整数组,包含字符串里每个单词 $words = str_word_count($str,1); ...
- Python统计日志中每个IP出现次数
介绍了Python统计日志中每个IP出现次数的方法,实例分析了Python基于正则表达式解析日志文件的相关技巧,需要的朋友可以参考下 本脚本可用于多种日志类型 #-*- coding:utf-8 -* ...
- Hashtable集合 --练习题_计算一个字符串中每个字符出现次数
Hashtable集合 java.util.Hashtable<K,V>集合 implements Map<K,V>接口 Hashtable:底层也是一个哈希表,是一个线程安 ...
- javascript 统计字符串中每个字符出现的次数
var str = "abdcadfasfdbadfafdasdfasyweroweurowqrewqrwqrebwqrewqrejwq;;"; // console.log(nu ...
- Java中统计字符串中各个字符出现的次数
import java.util.Iterator; import java.util.Set; import java.util.TreeMap; public class TreeMapDemo ...
随机推荐
- 安装在谷歌axure小工具
下载插件 第一步 第二步 第三步 第四步 版权声明:本文博客原创文章.博客,未经同意,不得转载.
- 在微信浏览器里使用js或jquery实现页面重新刷新
function refresh() { var random = Math.floor((Math.random() * 10000) + 1); var url = decodeURI(windo ...
- jquery li练习2-恢复链条
<!DOCTYPE html><html lang="en" xmlns="http://www.w3.org/1999/xhtml"> ...
- WPF 列表样式
<Window x:Class="CollectionBinding.MainWindow" xmlns="http://schemas.micros ...
- Hermite曲线插值
原文 Hermite Curve Interpolation Hermite Curve Interpolation Hamburg (Germany), the 30th March 1998. W ...
- c#定时调用作业
1.在Global.asax文件中添加启动线程 protected void Application_Start(object sender, EventArgs e) { Thread ThServ ...
- 在 Laravel 中通过 Artisan View 扩展包创建及删除应用视图文件
1.简介 本扩展包添加了两个视图相关的Artisan命令到Laravel应用,以便我们通过Artisan命令即可创建和管理视图文件,可谓是进一步解放了生产力. 2.安装 还是通过Composer安装: ...
- 生成wsdl代理c#代码
C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6.1 Tools\wsdl.exe /l:CS /out:d:\ws ...
- Win8Metro(C#)数字图像处理--2.26图像减法
原文:Win8Metro(C#)数字图像处理--2.26图像减法 [函数名称] 图像减法函数SubtractionProcess(WriteableBitmap src, WriteableBi ...
- 【Windows10 IoT开发系列】PowerShell的相关配置
原文:[Windows10 IoT开发系列]PowerShell的相关配置 可使用 Windows PowerShell 远程配置和管理任何 Windows 10 IoT 核心版设备.PowerShe ...