I would like to ask why python2.7 and 3.6 use a for loop to output dictionary contents, so why are they sorted differently?
PHP中文网
PHP中文网 2017-05-18 10:45:20
0
2
590

Excuse me, why are the dictionary contents sorted differently when using the same for in 2.7 and 3.6?

d = {'Adam':95,'Lisa':85,'Bart':59}
for k,v in d.items():
  print k,':',v
  #3.6的是print(k,':',k)

2.7 Output content

Lisa : 85
Adam : 95
Bart : 59

And 3.6 displays normally

Adam:95
Lisa:85
Bart:59
PHP中文网
PHP中文网

认证高级PHP讲师

reply all(2)
某草草

Don’t worry too much about why the dictionary is sorted differently. Dictionaries are inherently unordered. If you want them to be ordered, you need to sort them before returning. In python3, such an operation is generally performed to reduce memory usage.

黄舟

Because of this

https://docs.python.org/3/wha...

Cython 3.6 changes the implementation of dict to improve performance, and the automatic sorting of key names is a small side effect.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template