Why are tuples constructed from differently initialized sets equal?

前端 未结 4 1083
无人共我
无人共我 2021-02-05 00:48

I expected the following two tuples

>>> x = tuple(set([1, \"a\", \"b\", \"c\", \"z\", \"f\"]))
>>> y = tuple(set([\"a\", \"b\", \"c\", \"z\", \         


        
4条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-05 01:42

    so you have two lists - which have the same content but in different orders, you convert them into sets - which will be equal, as they have the same content.

    When you convert those sets into tuples, they will be converted in the same order, as they are the same set, so the tuples will be the same.

    This is true in Python2.7 - but from 3.3 onwards when hashes are randomised you will not be able to guarantee this - as the two sets, although equal in content wont neccessarily iterate in the same order.

提交回复
热议问题