2014-10-27 81 views
-2

我已經完成了一個練習程序。它打印得非常好,但問題是輸出的最後三行不準確。每次運行程序時,它都會生成不同的輸出,並在鍵之間交替。我不能找到一個辦法把它出來的權利(如寫入的程序中)Python字典程序輸出問題

def main(): 
    zip_dictionary = {} 
    zip_dictionary = update_dictionary(zip_dictionary, "CAM", "1427") 
    zip_dictionary = update_dictionary(zip_dictionary, "UofM", "1427") 
    zip_dictionary = update_dictionary(zip_dictionary, "ZIT", "1423-503") 
    zip_dictionary = update_dictionary(zip_dictionary, "MCOC", "1423") 
    zip_dictionary = update_dictionary(zip_dictionary, "NAZARETH", "1418") 
    zip_dictionary = update_dictionary(zip_dictionary, "OCC", "1420") 

    for key in zip_dictionary: 
     print(key, zip_dictionary[key]) 

    for i in range(2): 
     print(zip_dictionary.popitem()) 

    for value in zip_dictionary: 
     print(zip_dictionary[value], key) 

def update_dictionary(dct, key, value): 
    dct[key] = value 
    return dct 
main() 
+4

詞典是*無序*。你究竟想達到什麼目的? – jonrsharpe 2014-10-27 18:23:06

+0

你最後的'for'循環對我來說看起來很陌生。你打算在那裏使用「鑰匙」嗎?因爲'key'的值在遍歷'value'時不會改變。它會持續指向'key'從第一個循環開始的最後一個值。 – Kevin 2014-10-27 18:28:55

+0

請**編輯問題**,允許您將其設置爲可讀格式。此外,你期望得到的東西可能會有所幫助。 – jonrsharpe 2014-10-27 18:32:52

回答

1

使用的OrderedDict,不打擾update_dictionary,並且當你遍歷value不打印key

from collections import OrderedDict 

def main(): 
    zip_dictionary = OrderedDict() 
    zip_dictionary["CAM"] = "1427" 
    zip_dictionary["UofM"] = "1427" 
    zip_dictionary["ZIT"] = "1423-503" 
    zip_dictionary["MCOC"] = "1423" 
    zip_dictionary["NAZARETH"] = "1418" 
    zip_dictionary["OCC"] = "1420" 

    print "\nContents of dict:" 
    for key in zip_dictionary: 
     print(key, zip_dictionary[key]) 

    print "\nPopping items:" 
    for i in range(2): 
     print(zip_dictionary.popitem()) 

    print "\nRemaining items:" 
    for key in zip_dictionary: 
     print(key, zip_dictionary[key]) 

main() 

結果:

Contents of dict: 
('CAM', '1427') 
('UofM', '1427') 
('ZIT', '1423-503') 
('MCOC', '1423') 
('NAZARETH', '1418') 
('OCC', '1420') 

Popping items: 
('OCC', '1420') 
('NAZARETH', '1418') 

Remaining items: 
('CAM', '1427') 
('UofM', '1427') 
('ZIT', '1423-503') 
('MCOC', '1423') 
+0

它做到了。非常感謝。 @Kevin – Miteux 2014-10-27 19:32:53

0

如果你只想得到「相同的」輸出每次可以按鍵排序時首先基於字母訂購。

for sorted_key in [key in zip_dictionary].sort(): 
    print(sorted_key , zip_dictionary[sorted_key]) 

如果他們需要是按照這個順序,你也可以保持與鍵的有序列表。您也不需要返回update_dictionary函數中的字典,因爲它是一個可變對象。

def update_dictionary(dct, lst, key, value): 
    dct[key] = value 
    if not key in lst: 
     lst.append(key) 

def main(): 
    zip_dictionary = {} 
    zip_list = [] 
    update_dictionary(zip_dictionary, zip_list, "CAM", "1427") 
    update_dictionary(zip_dictionary, zip_list, "UofM", "1427") 
    update_dictionary(zip_dictionary, zip_list, "ZIT", "1423-503") 
    update_dictionary(zip_dictionary, zip_list, "MCOC", "1423") 
    update_dictionary(zip_dictionary, zip_list, "NAZARETH", "1418") 
    update_dictionary(zip_dictionary, zip_list, "OCC", "1420") 

    for key in zip_list: 
     print(key, zip_dictionary[key])