2012-02-29 70 views
7

我試圖打印出Python中的字典:如何在Python中實現關聯數組(不是字典)?

Dictionary = {"Forename":"Paul","Surname":"Dinh"} 
for Key,Value in Dictionary.iteritems(): 
    print Key,"=",Value 

雖然項目「用的名字」先上市,但在Python 字典似乎通過值排序,所以結果是這樣的:

Surname = Dinh 
Forename = Paul 

如何打印出這些用代碼或順序相同的順序時,項(不是值也不是由關鍵字排序)被附加?

+3

你可以使用按鍵額外的列表,以確定我在考慮的順序 – Nobody 2012-02-29 08:45:50

+0

使用2個列表(python數組),然後使用「枚舉」,但這很複雜 – jondinham 2012-02-29 08:48:03

+3

您是否還需要能夠通過密鑰快速訪問它? – 2012-02-29 08:50:44

回答

15

您可以使用元組列表(或列表列表)。就像這樣:

Arr= [("Forename","Paul"),("Surname","Dinh")] 
for Key,Value in Arr: 
    print Key,"=",Value 

Forename = Paul 
Surname = Dinh 

你可以做一個字典出這有:

Dictionary=dict(Arr) 

而且正確排序鍵是這樣的:

keys = [k for k,v in Arr] 

那麼做到這一點:

for k in keys: print k,Dictionary[k] 

但我同意評論在你的問題上:當循環代替時按要求的順序排序鍵不是很容易嗎?

編輯:(謝謝你,裏克·波吉),OrderedDict爲您完成此:

od=collections.OrderedDict(Arr) 
for k in od: print k,od[k] 
+0

可以使用:Arr = [[「Forename」,「Paul」],[「Surname」,「Dinh」]]而不是? – jondinham 2012-02-29 08:51:59

+3

元組列表是如何實現OrderedDict的,如果字典是OP正在尋找的結構,那麼他應該使用它們並且每次都保存轉換的痛苦。 – 2012-02-29 09:25:44

+1

@RikPoggi。啊,補充說。 – 2012-02-29 09:40:47

3

「但在Python字典是由值排序」也許我錯了這裏,但什麼遊戲你那個ideea?字典不按任何排序。

您將有兩種解決方案,可以保留字典附加的鍵列表,也可以使用不同的數據結構,如數組或數組。

4

這可能會滿足您的需要更好:

Dictionary = {"Forename":"Paul","Surname":"Dinh"} 
KeyList = ["Forename", "Surname"] 
for Key in KeyList: 
    print Key,"=",Dictionary[Key] 
+0

但大多數時間我似乎並不知道密鑰列表 – jondinham 2012-02-29 09:05:17

+1

那麼你怎麼知道排序:) – 2012-02-29 09:07:06

+0

有一種情況:讓'Dictionary'通過函數傳回給我。並且我想保留該函數創建的值的順序(例如,通過「append」) – jondinham 2012-02-29 09:13:17

16

首先,字典根本沒有排序,也沒有按鍵或按價值排序。

並根據您的描述。您actualy需要collections.OrderedDict模塊

from collections import OrderedDict 

my_dict = OrderedDict([("Forename", "Paul"), ("Surname", "Dinh")]) 

for key, value in my_dict.iteritems(): 
    print '%s = %s' % (key, value) 

請注意,您需要從元組的列表實例OrderedDict不是來自另一個字典作爲字典實例將洗牌項目的順序OrderedDict將被實例化之前。

+0

因此,從Python 3.6開始,dicts會保持按鍵順序。 – lig 2017-11-09 21:40:13

3

我不知道這是一個有序字典要:

>>> k = "one two three four five".strip().split() 
>>> v = "a b c d e".strip().split() 
>>> k 
    ['one', 'two', 'three', 'four', 'five'] 
>>> v 
    ['a', 'b', 'c', 'd', 'e'] 
>>> dx = dict(zip(k, v)) 
>>> dx 
    {'four': 'd', 'three': 'c', 'five': 'e', 'two': 'b', 'one': 'a'} 
>>> for itm in dx: 
     print(itm) 

    four 
    three 
    five 
    two 
    one 

>>> # instantiate this data structure from OrderedDict class in the Collections module 
>>> from Collections import OrderedDict 
>>> dx = OrderedDict(zip(k, v)) 
>>> for itm in dx: 
     print(itm) 

    one 
    two 
    three 
    four 
    five 

字典使用OrderdDict 保留原始的插入順序創建。換句話說,這樣的字典根據它們被插入的順序在鍵/值對上進行迭代。

因此,舉例來說,當你刪除密鑰,然後重新添加相同的密鑰,迭代順序變化:

>>> del dx['two'] 
>>> for itm in dx: 
     print(itm) 

     one 
     three 
     four 
     five 

>>> dx['two'] = 'b' 
>>> for itm in dx: 
     print(itm) 

     one 
     three 
     four 
     five 
     two 
+0

爲什麼不使用問題中的代碼? – lig 2012-02-29 09:04:48

+0

是否有必要讓你理解我的答案?事實上,我沒有使用OP的樣本數據有兩個原因:(i)我們在這裏討論有序序列,而OP的數據只有*兩個*鍵值對; (ii)我爲我的樣本數據選擇了關鍵字和值,這樣讀者就可以輕鬆看到我的代碼點 - 即保存命令。 – doug 2012-02-29 09:07:25