import time
sector = 100
dict = {
0: {"sector1": '', 'time': ''},
1: {"sector2": '', 'time': ''}
}
for index in range(2):
dict[index].update({'sector'+str(index+1): sector, 'time': time.perf_counter()})
sector += 100
print(dict)
我有这段代码,在上面的代码中,我意外地看到字典键和值被随机交换,或者我应该经常说。
这是我的意思
{
0: {'sector1': 100, 'time': 80069.410857487},
1: {'sector2': 200, 'time': 80069.410862547}
}
以上输出为预期结果
{
0: {'time': 79298.567438224, 'sector1': 100},
1: {'time': 79298.567442553, 'sector2': 200}
}
但是我会随机获得上面的输出
是什么原因?如果我的字典会包含大量数据,它会有任何潜在的问题吗?
答案 0 :(得分:2)
Python dict
是无序的,因此内部存储可以是任何顺序。如果您想订购字典,请使用collections.OrderedDict