问题描述:

This question already has an answer here:

  • Python dictionary, how to keep keys/values in same order as declared?

    10 answers

网友答案:

As mentioned in the comments, you need to use an OrderedDict if you want to rely on the ordering of elements in your dictionary:

>>> from collections import OrderedDict
>>> OrderedDict(zip(a, b))
OrderedDict([('abc', ''), ('def', ''), ('ghi', ['123', '456', '786', '989']), ('jkl', ''), ('mno', ['222', '888', '111', '333'])])

It can be accessed in the same way as a normal dict:

>>> x = OrderedDict(zip(a, b))
>>> x['abc']
''
相关阅读:
Top