Why dictionary values aren't in the inserted order?

前端 未结 6 857
野的像风
野的像风 2020-12-19 02:37

When i declare a list 1,2,3,4 and i do something with it , even just print i get back the same sequence 1,2,3,4.

But when i do anything with dictionaries , they alw

6条回答
  •  悲&欢浪女
    2020-12-19 02:40

    If you want to see the entries in order. something like:

    test2 = {"c":3,"a":1,"b":2,"d":4}
    ks = test2.keys()
    ks.sort()
    for key in ks:
       print key + ':' + str(test2[key])
    

    (cut,paste, season to taste)

提交回复
热议问题