【发布时间】:2019-08-29 11:12:10
【问题描述】:
我收到一个我无法解决的类型错误
这是为字典构造一个计数器:
counts = dict()
names = ['csev','cwen', 'csev', 'zqian', 'cwen']
#makes new tally for new names and updates existing names
for name in names :
if name not in counts:
counts[name] = 1
else:
counts[name] = counts[name + 1]
print(counts)
应该输出:
{'csev':2, 'zqian':1, 'cwen':2}
【问题讨论】:
-
错字?:
counts[name + 1]vscounts[name] + 1 -
我没有关注你
-
您想将
1添加到count[name],而不是添加到name,对吗?这就是为什么我认为你只是记错了。
标签: python dictionary histogram counting names