您的位置:首页 > 编程语言 > Python开发

python找出字符串中每个字母出现的次数

2017-03-01 22:17 405 查看
#完全用列表方式实现
#!/usr/bin/env python
# coding=utf-8
str1 = "abcdefabcdefgghj"
listStr = []

for eachStr in str1:
countStr = str1.count(eachStr)
numStr = eachStr + ":" + str(countStr)
if numStr not in listStr :
listStr.append(numStr)

for i in listStr:
print i

############用字典方式实现################
#!/usr/bin/python
# coding=utf-8
d = {}

for s in ['c','c','c++','python','python','python','js']:
if s in d:
d[s] = d[s] + 1
else:
d[s] = 1

for key in d:
print('%s count is %d')%(key,d[key])
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  字符串 统计 python