python 如何实现 Key 对应的 value 不断加一?

2016-09-05 16:01:03 +08:00
 Newyorkcity

record = {'example' : 0}
for i in range(0,10):
   record['example'] = record['example']所对应的 value+1

关键问题在于「 record['example']所对应的 value 」该怎么写?

2776 次点击
所在节点    Python
6 条回复
loveersoldier
2016-09-05 16:06:52 +08:00
record['example'] += 1
NaVient
2016-09-05 17:12:45 +08:00
record['example']就是对应的 value 啊
beviniy
2016-09-06 13:37:52 +08:00
from itertools import Counter
tumbzzc
2016-09-06 19:22:28 +08:00
突然觉得很囧
wind3110991
2016-09-12 23:19:42 +08:00
record = {'example' : 0}
for i in range(0,10):
   record['example'] = record['example'] + 1

print record['example']
romanticbao
2016-09-17 02:56:58 +08:00
record = {}
DEFAULT_VALUE = 0
for item in data_list:
record[item] = record.get(item, DEFAULT_VALUE) + 1

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/304061

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX