Are sets ordered like dicts in python3.6

前端 未结 2 1390
忘了有多久
忘了有多久 2020-11-27 18:35

Due to changes in dict implementation in Python 3.6 it is now ordered by default. Do sets preserve order as well now?

I could not find any

2条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-27 19:15

    sets are not ordered in Python 3.6, not even as a CPython implementation detail. A simple example illustrates this:

    >>> import string
    >>> string.digits
    '0123456789'
    >>> set(string.digits)
    {'7', '0', '2', '8', '6', '9', '1', '5', '4', '3'}
    

    The Python 3 docs are clear on this:

    A set is an unordered collection with no duplicate elements.

提交回复
热议问题