V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
Newyorkcity
V2EX  ›  Python

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

  •  
  •   Newyorkcity · 2016-09-05 16:01:03 +08:00 · 2756 次点击
    这是一个创建于 2792 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

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

    6 条回复    2016-09-17 02:56:58 +08:00
    loveersoldier
        1
    loveersoldier  
       2016-09-05 16:06:52 +08:00
    record['example'] += 1
    NaVient
        2
    NaVient  
       2016-09-05 17:12:45 +08:00
    record['example']就是对应的 value 啊
    beviniy
        3
    beviniy  
       2016-09-06 13:37:52 +08:00
    from itertools import Counter
    tumbzzc
        4
    tumbzzc  
       2016-09-06 19:22:28 +08:00
    突然觉得很囧
    wind3110991
        5
    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
        6
    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
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4391 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 574ms · UTC 10:10 · PVG 18:10 · LAX 03:10 · JFK 06:10
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.