我有一个 Python 函数的一部分,如下所示:
for item in passedList:
tempDict = {}
tempDict ["first"] = item[0]
tempDict ["second"] = item[1]
tempDict ["third"] = item[2]
我期待的是:
{'first': 'item1', 'second': 'item2', 'third': 'item3'}
但是,我得到:
{'second': 'item2', 'first': 'item1', 'third': 'item3'}
这可能是一个非常简单的疏忽,但有没有想过为什么会这样?
请您参考如下方法:
这是因为 implementation of dict在 Python 中是 HashMap 或哈希表,它不按顺序存储元素。
你可以使用 OrderedDict来解决这个问题。
