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

python3 中 set 进行列表去重时 为什么会自动进行排序(升序)

  •  
  •   mili8908 · 2018-09-06 21:15:39 +08:00 · 4157 次点击
    这是一个创建于 2304 天前的主题,其中的信息可能已经有所发展或是发生改变。
    In [35]: set([1, 3, 8, -2, 99, 98, 77, 1, 5, 3, 77, 12])
    Out[35]: {-2, 1, 3, 5, 8, 12, 77, 98, 99}


    In [31]: o = [2,5,1,2,4,6,0,33,5.4,6.4]

    In [32]: c = set(o)

    In [33]: c
    Out[33]: {0, 1, 2, 4, 5, 5.4, 6, 6.4, 33}
    3 条回复    2020-01-02 11:13:19 +08:00
    GTim
        1
    GTim  
       2018-09-06 21:24:40 +08:00
    楼主多试一试几次

    即使你看到了十次都是有序的,也不要相信,`set` 是无序的 `set` 是无序的 `set` 是无序的
    PythonAnswer
        2
    PythonAnswer  
       2018-09-06 21:32:39 +08:00
    楼主多看文档

    5.4. Sets

    Python also includes a data type for sets. A set is an unordered collection with no duplicate elements. Basic uses include membership testing and eliminating duplicate entries. Set objects also support mathematical operations like union, intersection, difference, and symmetric difference.

    Curly braces or the set() function can be used to create sets. Note: to create an empty set you have to use set(), not {}; the latter creates an empty dictionary, a data structure that we discuss in the next section.
    drawstar
        3
    drawstar  
       2020-01-02 11:13:19 +08:00
    @PythonAnswer 从文档能看出啥?无序的?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   941 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 21:59 · PVG 05:59 · LAX 13:59 · JFK 16:59
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.