Set iteration order varies from run to run

前端 未结 7 1304
春和景丽
春和景丽 2020-12-01 17:54

Why does the iteration order of a Python set (with the same contents) vary from run to run, and what are my options for making it consistent from run to run?

I under

7条回答
  •  旧巷少年郎
    2020-12-01 18:47

    The set's iteration order depends not only its contents, but on the order in which the items were inserted into the set, and whether there were deletions along the way. So you can create two different sets, using different insertions and deletions, and end up with the same set at the end, but with different iteration orders.

    As others have said: if you care about the order of the set, you have to create a sorted list from it.

提交回复
热议问题