【发布时间】:2015-05-20 15:23:57
【问题描述】:
我正在尝试计算字符串中每个字符的数量
我的代码在这里:
def CharCount(string):
mydict = {}
for char in string:
mydict[char] = mydict.get(char) + 1
return '\n'.join(['%s,%s' % (c, n) for c, n in mydict.items()])
if __name__ == '__main__':
print CharCount("abcda")
在运行上面的代码时出现以下错误:
TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'
【问题讨论】:
-
None是dict.get(char)返回的内容
标签: python python-2.7