统计一个字符串中各个字符个数

# 统计一个字符串中各个字符个数
#
# words="hello word"
# wordsCount={}
# for i in words:
#     if i!=" ":
#         wordsCount[i]=0
# for i in words:
#     if i != " ":
#         wordsCount[i] +=1
# print(wordsCount)

a = "hello"
b = dict.fromkeys(a, 0)
print(b)
print("b[o] :",b["o"])
# print(b)
for x in a:
    b[x]+=1
    # print(b[x])
print(b)

 

posted @ 2018-08-09 11:44  studylady  阅读(414)  评论(0编辑  收藏  举报