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
oldbird
V2EX  ›  Python

字典.keys()获得的键列表元素顺序是随机的吗?

  •  
  •   oldbird · 2020-01-02 09:03:17 +08:00 · 6551 次点击
    这是一个创建于 1566 天前的主题,其中的信息可能已经有所发展或是发生改变。

    python2,dict 对象的 keys()方法获得的键列表中的元素顺序是固定的还是随机的? 用少量数据试了下,看上去好像是固定且有规律的(升序?) dict.values()是按 keys()结果的顺序输出的?

    19 条回复    2020-01-02 20:57:48 +08:00
    imn1
        1
    imn1  
       2020-01-02 09:05:57 +08:00
    3.5 后是依次加入的顺序
    d['b']=1
    d['a']=2
    顺序就是 b a
    sikariba
        2
    sikariba  
       2020-01-02 09:11:02 +08:00
    我怎么记得是 3.7,PyPy 不记得,CPython 是 3.6 实现的,然后是到 3.7 才推广到全语言的
    lihongjie0209
        3
    lihongjie0209  
       2020-01-02 09:16:20 +08:00
    Python 不知道, 但是在 Java 中, 为了避免客户依赖这种特性, 哪怕是内部有序的, 返回给你的时候都会打乱.

    既然使用 hash 表, 就不要再考虑顺序了
    jdhao
        4
    jdhao  
       2020-01-02 09:18:33 +08:00 via Android
    随机的,需要顺序,用 ordered dict
    love
        5
    love  
       2020-01-02 09:31:26 +08:00 via Android
    @lihongjie0209 hash 顺序挺有用,只要库明确支持就可以放心用,比如我大 js 明确可以依赖 key 顺序
    drawstar
        6
    drawstar  
       2020-01-02 09:33:56 +08:00
    我记得 python 中好像是随机的
    josephpei
        7
    josephpei  
       2020-01-02 09:36:04 +08:00
    Python 3.7+ https://docs.python.org/3.7/whatsnew/3.7.html

    In Python 3.7.0 the insertion-order preservation nature of dict objects has been declared to be an official part of the Python language spec. Therefore, you can depend on it.
    josephpei
        8
    josephpei  
       2020-01-02 09:38:19 +08:00
    go 之前版本是键列表是有序的,后来为了避免大家依赖这个,新版本改成无序的了
    hjq98765
        9
    hjq98765  
       2020-01-02 10:22:36 +08:00
    不是随机的,感觉是排过序的:

    >>> {1:2,3:4}.keys()
    >>> [1, 3]

    --------------------

    >>> {3:2,1:4}.keys()
    >>> [1, 3]
    tkmiles
        10
    tkmiles  
       2020-01-02 10:24:23 +08:00
    看版本, 3.6 之前是"无序"的, 也就是 hash 顺序

    3.6 之后则是按 key 插入顺序
    axer0912
        11
    axer0912  
       2020-01-02 10:30:32 +08:00 via Android
    dart 有一个叫 LinkedHashMap,那是有顺序的
    tfdetang
        12
    tfdetang  
       2020-01-02 10:30:55 +08:00
    从 python3.6 起就是有序的了,之前都不是有序的
    fank99
        13
    fank99  
       2020-01-02 10:53:21 +08:00
    虽然有 order dict,但内部是有序的。
    如同上面老哥所述,在 3.x ( x 是几我给忘了)后的版本是有序的
    littlewey
        14
    littlewey  
       2020-01-02 10:54:37 +08:00
    建议你把自己的问题变成关键词,Google 一下。
    python dict keys ordering
    ibreaker
        15
    ibreaker  
       2020-01-02 11:10:41 +08:00
    @littlewey。。。。。
    nongmei
        16
    nongmei  
       2020-01-02 11:56:55 +08:00
    记得 python 的字典是哈希存储,有顺序,但不是你插入的顺序
    crella
        17
    crella  
       2020-01-02 14:05:18 +08:00 via Android
    ruby2.6.4 是按插入的顺序
    crella
        18
    crella  
       2020-01-02 14:07:10 +08:00 via Android
    2.5.7 也是。不过为什么会出现需要 hash 的键自动排序的情况呢?
    oldbird
        19
    oldbird  
    OP
       2020-01-02 20:57:48 +08:00
    在 py2.7 试了输出任意次 keys(),每次输出的列表都是一样的,可能应该这样理解:keys()结果只有一种固定的排列情况,这个固定的序列中元素的顺序是任意的。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1160 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 18:24 · PVG 02:24 · LAX 11:24 · JFK 14:24
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.