我有以下形式的数据
00 154
01 72
02 93
03 202
04 662
05 1297
00 256
我希望遍历每一行,并将第1列中的值作为键,将第2列的值作为值
另外,如果当前键已经存在,则在数学上将第2列的新值添加到第2列的当前值上。在
试过这个:
^{pr2}$
得到了这个:
Traceback (most recent call last):
File "./get_idmanager_stats.py", line 25, in <module>
if d[a]:
KeyError: '00'
Tags:
这就是
collections.defaultdict
的用途。在你可以简单地做
以及
^{pr2}$您将发现它不需要任何
if
语句就可以工作。在相关问题 更多 >
编程相关推荐