简体   繁体   English

为什么 py2 和 py3 中元素的顺序不同?

[英]Why does order of elements in a set differ in py2 and py3?

In py2:在 py2 中:

{1: {“1”, “2”, “7”, “3”, “4”, “9”, “8”}}

Returns for all runs {1: set(['1', '3', '2', '4', '7', '9', '8'])}返回所有运行{1: set(['1', '3', '2', '4', '7', '9', '8'])}

In py3:在 py3 中:

{1: {“1”, “2”, “7”, “3”, “4”, “9”, “8”}}

Returns for first run {1: {“1”, “3”, “4”, “7”, “8”, “2”, “9”}}返回第一次运行{1: {“1”, “3”, “4”, “7”, “8”, “2”, “9”}}

Returns for second run {1: {“2”, “4”, “9”, “7”, “1”, “3”, “8”}} Order of items in inside dict keeps changing.返回第二次运行{1: {“2”, “4”, “9”, “7”, “1”, “3”, “8”}} dict 中的项目顺序不断变化。

My main question is why does it keep changing with every run and how to resolve it so that returns a stable result as in py2?我的主要问题是为什么它会随着每次运行而不断变化,以及如何解决它以便返回一个稳定的结果,就像在 py2 中一样?

Use a different structure (like a list or dict (since Python 3.7)/OrderedDict) - sets inherently don't have a concept of order.使用不同的结构(如列表或字典(自 Python 3.7 起)/OrderedDict)——集合本身没有顺序的概念。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM