Python中dict按照插入顺序输出,这到底是什么原因?
本人Python初学者,最近学习ordereddict有些困惑。现有如下代码:importcollectionsprint('Regulardictionary:')d=...
本人Python初学者,最近学习ordereddict有些困惑。
现有如下代码:
import collections
print ('Regular dictionary:')
d = {}
d['a'] = 'A'
d['b'] = 'B'
d['c'] = 'C'
d['d'] = 'D'
d['e'] = 'E'
for k, v in d.items():
print (k, v)
网上好几个博客说输出是无序版的,理由是字典不会记住键值对的插入顺序:
Regular dictionary:
a A
c C
b B
e E
d D
但是,重点来了!我在Python3.6中copy了这段代码但是输出是有序的,这是什么原因啊?
Regular dictionary:
a A
b B
c C
d D
e E
Regular dictionary:
a A
b B
c C
d D
e E 展开
现有如下代码:
import collections
print ('Regular dictionary:')
d = {}
d['a'] = 'A'
d['b'] = 'B'
d['c'] = 'C'
d['d'] = 'D'
d['e'] = 'E'
for k, v in d.items():
print (k, v)
网上好几个博客说输出是无序版的,理由是字典不会记住键值对的插入顺序:
Regular dictionary:
a A
c C
b B
e E
d D
但是,重点来了!我在Python3.6中copy了这段代码但是输出是有序的,这是什么原因啊?
Regular dictionary:
a A
b B
c C
d D
e E
Regular dictionary:
a A
b B
c C
d D
e E 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询