-4
我有2個列表,當我嘗試將它們轉換爲字典我的輸出是隨機的任何人都可以幫忙嗎?如何將列表轉換爲Python中的字典?
a=['abc', 'def', 'ghi', 'jkl', 'mno']
b=['', '', ['123', '456', '786', '989'], '', ['222', '888', '111', '333']]
print(dict(zip(a,b)))
output: {'def': '', 'ghi': ['123', '456', '786', '989'], 'jkl': '', 'abc': '', 'mno': ['222', '888', '111', '333']}
what i want is
{'abc':'', 'def':'', 'ghi':['123', '456', '786', '989'],'jkl':'','mno':['222', '888', '111', '333']}
您將需要一個'OrderedDict'做到這一點。 Python中不排序Plain Dicts。 –
字典未訂購。如果你需要訂單(你呢,真的嗎?),使用'OrderedDict'。 – jonrsharpe
Python字典(在Python 3.6之前)本質上是無序的。如果你想保存命令,可以使用'collections.OrderedDict'或者使用Python 3.6(但它仍然處於Beta版)。 – Duncan