Set iteration order varies from run to run

前端 未结 7 1290
春和景丽
春和景丽 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:30

    Contrary to sets, lists have always a guaranteed order, so you could toss the set and use the list.

    0 讨论(0)
  • 2020-12-01 18:42

    Use the symmetric_difference (^) operator on your two sets to see if there are any differences:

    In [1]: s1 = set([5,7,8,2,1,9,0])
    In [2]: s2 = set([9,0,5,1,8,2,7])
    In [3]: s1
    Out[3]: set([0, 1, 2, 5, 7, 8, 9])
    In [4]: s2
    Out[4]: set([0, 1, 2, 5, 7, 8, 9])
    In [5]: s1 ^ s2
    Out[5]: set()
    
    0 讨论(0)
  • 2020-12-01 18:45

    You can set the expected result to be also a set. And checks if those two sets are equal using ==.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-01 18:52

    Your question transformed into two questions: A) how to compare "the output of two runs" in your specific case; B) what's the definition of the iteration order in a set. Maybe you should distinguish them and post B) as a new question if appropriate. I'll answer A.

    IMHO, using a sorted list in your case is not a very clean solution. You should decide whether you care for iteration order once and for all and use the appropriate structure.

    Either 1) you want to compare the two sets to see if they have equal content, irrespective of the order. Then the simple == operator on sets seems appropriate. See python2 sets, python3 sets.

    Or 2) you want to check whether the elements were inserted in the same order. But this seems reasonable only if the order of insertion somehow matters to the users of your library, in which case using the set type was probably inappropriate to begin with. Put another way, it is unclear what you mean exactly by "comparing the output of two runs" and why you want to do that.

    In all cases, I doubt a sorted list is appropriate here.

    0 讨论(0)
  • 2020-12-01 18:54

    What you want isn't possible. Arbitrary means arbitrary.

    My solution would be the same as yours, you have to sort the set if you want to be able to compare it to another one.

    0 讨论(0)
提交回复
热议问题