【发布时间】:2017-10-16 04:26:45
【问题描述】:
我一直在尝试做的是对“字典”的值求和。我得到的错误是 'str' object is not callable。
根据我对 Stack Overflow 的研究,这是因为字典的某些值是字符串。但我认为我在循环中引入的条件语句会隔离这些字符串,因为它们不符合这个条件。我想创建一个新的字典,其中只有发生相同 TypeError 的 int 值。
有人有什么想法吗?
dictionary = {'a':1, 'b':2, 'c':'letter c', 'd':'letter d'}
for v in dictionary.values():
if type(v) is int:
total = sum(v)
print(total)
【问题讨论】:
标签: python dictionary for-loop if-statement typeerror