这个python片段有什么问题:
for zhszam in pontok.keys():
s = 0
for p in pontok[zhszam]:
if p.isdigit():
s += int(p)
print s
pontok[zhszam] = s
return pontok
其中pontok是{1: ['10', ' 5', ' 3', ' 10', ' 7'], 2: ['10', ' 5', ' 3', ' 10']}
。
它以某种方式给出了以下错误的输出:
^{pr2}$
而值应该是数字的总和。在
提前谢谢!在
Tags:
我宁愿发表评论,也不愿留下这个作为答案,但我还没有代表。这个问题将帮助您剥离那些前导空格:Python remove all whitespace in a string
除了第一个
'10'
之外,每个字符串都有一个前导空格,而不是数字。所以根本就没有被处理。在尝试:
您不应该使用
str.isdigit
,它很容易损坏。最好使用带有int()
的try except块。在相关问题 更多 >
编程相关推荐