遍历字典

时间:2019-07-10 20:33:12

标签: python-3.x python-2.7 dictionary

当我在leetcode中练习问题时,我发现遍历字典时Py2和Py3之间存在一些差异。例如,当我在Py3中运行以下代码时,键和值将按dict1的顺序依次打印出来,例如(1、2),(3、5),(7、8),(9 ,6)。但是,当我在dict2中运行相同的代码时,它会显示(7,8),(1,2),(3,5),(9,6)。为什么会这样?如何在Py2中将其作为原始序列打印?谢谢

dict1 = {1:2, 3: 5, 9: 6, 7: 8}
for each in dict1:
  print(each, dict1[each])

0 个答案:

没有答案