#coding:UTF-8
'''
Created on 2014年8月15日
@author: long.he
'''
print "method 1:"
str = "here is my name : stone.H"
dict_s = {}
for s in str:
if s in dict_s:
dict_s[s] += 1
else:
dict_s[s] = 1
print dict_s
print "method 2:"
dic = {}
for s in str:
dic.update({s:dic.get(s,0)+1})
print dic
print "method 3:"
print reduce(lambda d,s : dict(d, **{s:d.get(s,0)+1}),"here is my name : stone.W",{})
统计字符串中字符个数一些有趣的事
最新推荐文章于 2023-02-09 23:56:47 发布