推荐学习书目
› 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
yyy32
V2EX  ›  Python

初学者的 Python 问题,自己想不明白

  •  
  •   yyy32 · Feb 24, 2017 · 3107 views
    This topic created in 3523 days ago, the information mentioned may be changed or developed.
    d={‘ a ’=1,'b'=5,'c'=2}
    for value in d.itervalue():
    print value
    这个输出的是 1 2 5
    而 d={‘ a ’=1,'c'=5,'b'=2}
    for value in d.itervalue():
    print value
    这输出的是 1,5,2
    这是为什么呢
    9 replies  •  2017-02-25 09:16:25 +08:00
    staticor
        1
    staticor  
       Feb 24, 2017
    问题是 dict 迭代的顺序问题么

    首先要搜一下 dict 的输出是按什么顺序.

    然后还可以进一步, 看自己构造的 2 个 dict 用相同 k-v, 是一样的对象?
    yyy32
        2
    yyy32  
    OP
       Feb 24, 2017
    @staticor 哦 是因为 dict 是无序的吗
    guyskk
        3
    guyskk  
       Feb 24, 2017 via Android
    @yyy32 python3.6 之前,dict 是无序的, 3.6 改了实现,是有序的
    wohenyingyu02
        4
    wohenyingyu02  
       Feb 24, 2017 via iPhone
    和 itervalue 怎么实现的有关吧,看楼主 print 的架势似乎是 python2 ?
    kimoCHG
        5
    kimoCHG  
       Feb 24, 2017
    关于遍历的顺序, 一般语言都会写上不保证顺序吧
    PythonAnswer
        8
    PythonAnswer  
       Feb 25, 2017 via Android
    老的 dict 是 hash ,无序。想要有顺序可以导入
    OrderedDict 。

    最新版本改成有序的了。
    yyy32
        9
    yyy32  
    OP
       Feb 25, 2017
    好的 谢谢各位了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   5907 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 43ms · UTC 03:18 · PVG 11:18 · LAX 20:18 · JFK 23:18
    ♥ Do have faith in what you're doing.