我在 python 中得到错误的输出
I am getting wrong output in python
Python 字典是一个无序、可变和索引的集合。没有重复的 members.But 当我 运行 下面的代码时,我看到令人惊讶的错误输出(有序元组),它应该是无序的。代码有什么问题?
my_dict = {}
my_dict['a'] = 1
my_dict['b'] = 2
my_dict['c'] = 3
my_dict['d'] = 4
my_dict['e'] = 5
my_dict['f'] = 6
my_dict['g'] = 7
my_dict['h'] = 8
my_dict['i'] = 9
for k, v in my_dict.items():
print(k, v)
在 CPython 3.6 和(任何)Python 3.7 中,字典 remember their key insertion order 和对 dict.keys
、dict.values
和 dict.items
的调用将反映该顺序。
此外,"ordered by coincidence" 包含在您在早期实现中打印时可能获得的所有任意顺序的集合中。 :)
Python 字典是一个无序、可变和索引的集合。没有重复的 members.But 当我 运行 下面的代码时,我看到令人惊讶的错误输出(有序元组),它应该是无序的。代码有什么问题?
my_dict = {}
my_dict['a'] = 1
my_dict['b'] = 2
my_dict['c'] = 3
my_dict['d'] = 4
my_dict['e'] = 5
my_dict['f'] = 6
my_dict['g'] = 7
my_dict['h'] = 8
my_dict['i'] = 9
for k, v in my_dict.items():
print(k, v)
在 CPython 3.6 和(任何)Python 3.7 中,字典 remember their key insertion order 和对 dict.keys
、dict.values
和 dict.items
的调用将反映该顺序。
此外,"ordered by coincidence" 包含在您在早期实现中打印时可能获得的所有任意顺序的集合中。 :)