01.
<code
class
=
" hljs python"
>#! /usr/bin/env python
02.
#coding:utf-
8
03.
#定义一个函数,接收字符串,统计出每个字符的出现次数
04.
#实现思路:字典实现,看字符是否在字典中,不在则计入字典,否则+
1
。
05.
def histogram(str):
06.
dic = dict()
07.
for
c in str:
08.
if
c not in dic:
09.
dic[c] =
1
#add
10.
else
:
11.
dic[c]+=
1
#update
12.
return
dic
13.
print histogram(
'helloworld'
)
14.
<
联系客服