python字典迭代顺序问题

2018-09-28  本文已影响0人  泰兰德的加长香蕉

  Before Python 3.6 a regular dict did not track the insertion order, and iterating over it produced the values in order based on how the keys are stored in the hash table, which is in turn influenced by a random value to reduce collisions. In an OrderedDict, by contrast, the order in which the items are inserted is remembered and used when creating an iterator.

  Under Python 3.6, the built-in dict does track insertion order, although this behavior is a side-effect of an implementation change and should not be relied on.

use collections.OrderedDict instead!

上一篇 下一篇

猜你喜欢

热点阅读