当前位置: 动力学知识库 > 问答 > 编程问答 >

How do i convert list to dictionary in python?

问题描述:

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']
''
分享给朋友:
您可能感兴趣的文章:
随机阅读: