今天重新学习字段,书里有个内置函数的使用让我印象很深刻
setdefault(key,defaultvalue)方法
第一个参数,是要在字典中检查的键
第二个参数,是如果键不存在赋的值,如果该键存在,会返回该键的值。
然后作者运用这个方法在计算一个字段串字母的个数上
message = 'It was a bright cold day in April, and the clocks were striking thirteen.'
count = {}
for character in message:
count.setdefault(character, 0)
count[character] = count[character] + 1
print(count)