import pprint
x = 'hello,my name is Peter,and what is your name?'
count = {}
for chart in x:
count.setdefault(chart, 0)
count[chart] += 1
pprint.pprint(count)
字典中的setfault()方法:一般输入两个参数,第一个参数是要在字典中查询的键,第二个参数是当字典中没有该键时为了防止报错而将该值赋给第一个参数,并加入字典,如果字典中有该键值,则会传出该键值,第二个参数此时无作用。