Are sets ordered like dicts in python3.6

前端 未结 2 1368
忘了有多久
忘了有多久 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.

    0 讨论(0)
  • 2020-11-27 19:41

    No, sets are still unordered.

    You can verify this just by displaying a set that should have a "well-defined hash order"1 to make sure we don't accidentally get a set that looks ordered but actually isn't:

    >>> a_set = {3,2,1}
    >>> a_set
    {1, 2, 3}
    >>> list(a_set)
    [1, 2, 3]
    

    If it were ordered you would expect {3, 2, 1} and [3, 2, 1] as result of the examples.

    While dicts are actually ordered (same example just a bit modified):

    >>> a_dict = {3: 3, 2: 2, 1:1}
    >>> a_dict
    {3: 3, 2: 2, 1: 1}
    >>> list(a_dict)
    [3, 2, 1]
    

    1 "well-defined hash order":

    For integers that satisfy 0 <= integer < sys.hash_info.modulus the hash is just the number itself. That means if the set is ordered "based" on the hash (and not ordered based on the insertion "time") and the hash values don't collide (that's why I used small numbers and numbers that only differ by one) the order should be deterministic because they occupy slots inside the set that are next to each other:

    • Either from smallest to highest
    • or a from a specific value to the highest and then from the smallest to the specific value. This case happens if the next (in the sense of neighboring) free slot in the set is the first one.

    As an example for the latter:

    >>> a_set = {6,7,8,9}
    >>> a_set
    {8, 9, 6, 7}
    
    0 讨论(0)
提交回复
热议问题