2013-04-05 103 views
0

好了的輸出,所以我有以下的行星的詞典,並且每個行星都有包含其規範自己的字典:排序字典

d={ 
'Mercury':{ 
'Distance from the sun' : 58, 
'Radius' : 2439.7, 
'Gas planet?' : False, 
'Atmosphere?' : True, 
'Moons' : []}, 
'Jupiter':{ 
'Distance from the sun' : 483, 
'Radius' : 69911, 
'Gas planet?' : True, 
'Atmosphere?' : True, 
'Moons' : ['Io', 'Ganymede', 'Callisto', 'Europa', 'Adrastea']}, 
'Uranus':{ 
'Distance from the sun' : 3000, 
'Radius' : 25559, 
'Gas planet?' : True, 
'Atmosphere?' : True, 
'Moons' : ['Miranda', 'Ariel', 'Umbriel', 'Titania', 'Oberon']}, 
'Mars':{ 
'Distance from the sun' : 207, 
'Radius' : 3396.2, 
'Gas planet?' : False, 
'Atmosphere?' : True, 
'Moons' : ['Phobos', 'Deimos']}, 
'Earth':{ 
'Distance from the sun' : 150, 
'Radius' : 6371.0, 
'Gas planet?' : False, 
'Atmosphere?' : True, 
'Moons' : ['Moon']}, 
'Venus':{ 
'Distance from the sun' : 108, 
'Radius' : 6051.8, 
'Gas planet?' : False, 
'Atmosphere?' : True, 
'Moons' : []}, 
'Saturn':{ 
'Distance from the sun' : 1400, 
'Radius' : 60268, 
'Gas planet?' : True, 
'Atmosphere?' : True, 
'Moons' : ['Pan', 'Prometheus', 'Titan', 'Phoebe', 'Rhea']}, 
'Neptune':{ 
'Distance from the sun' : 4500, 
'Radius' : 24764, 
'Gas planet?' : True, 
'Atmosphere?' : True, 
'Moons' : ['Triton', 'Nereid', 'Proteus', 'Naiad', 'Thalassa']}}` 

從本質上講就是我想要做的就是打印它在它出現的順序在詞典中,所以所使用的代碼:

for planets in sorted(d.keys()): 
print(planets) 
    for k,v in sorted(d[planets].items()): 
     print(k, ":", v) 

然而這產生每個行星和密鑰值的每個行星的描述隨機順序。 (行星的名字,然後它的規格都印在它下面,當我在python運行它,我只是不知道如何格式化它來顯示它在堆棧方式)

即:

Neptune 
Moons : ['Triton', 'Nereid', 'Proteus', 'Naiad', 'Thalassa'] 
Radius : 24764 
Distance from the sun : 4500 
Gas planet? : True 
Atmosphere? : True 
Jupiter 
Moons : ['Io', 'Ganymede', 'Callisto', 'Europa', 'Adrastea'] 
Radius : 69911 
Distance from the sun : 483 
Gas planet? : True 
Atmosphere? : True 
Earth 
Moons : ['Moon'] 
Radius : 6371.0 
Distance from the sun : 150 
Gas planet? : False 
Atmosphere? : True 
Mercury 
Moons : [] 
Radius : 2439.7 
Distance from the sun : 58 
Gas planet? : False 
Atmosphere? : True 
Mars 
Moons : ['Phobos', 'Deimos'] 
Radius : 3396.2 
Distance from the sun : 207 
Gas planet? : False 
Atmosphere? : True 
Uranus 
Moons : ['Miranda', 'Ariel', 'Umbriel', 'Titania', 'Oberon'] 
Radius : 25559 
Distance from the sun : 3000 
Gas planet? : True 
Atmosphere? : True 
Venus 
Moons : [] 
Radius : 6051.8 
    Distance from the sun : 108 
    Gas planet? : False 
    Atmosphere? : True 
    Saturn 
    Moons : ['Pan', 'Prometheus', 'Titan', 'Phoebe', 'Rhea'] 
    Radius : 60268 
    Distance from the sun : 1400 
    Gas planet? : True 
    Atmosphere? : True 

我試過使用sorted(),但只是按字母順序排列。有什麼建議麼?

+0

你想要什麼命令? 「命令它出現在字典中」是什麼意思?字典不以任何順序保存元素。 – Jared 2013-04-05 03:24:26

+3

你不能這樣做,字典不是爲了排序,而是主要用於映射和檢索。你可以做的是構建一個元組列表(x,y),其中x是關鍵字,y是值。您可以使用循環按順序打印出鍵和值的元組列表,然後將該列表傳遞給'dict()',然後在按照該順序打印之後您將擁有該字典。 – eazar001 2013-04-05 03:47:36

+0

對於你自己的實現:http://stackoverflow.com/questions/60848/how-do-you-retrieve-items-from-a-dictionary-in-the-order-that-theyre-inserted – eazar001 2013-04-05 03:49:31

回答

0

這是無法完成的。 Python dictionaries對他們的打印訂單不作任何承諾。

0

它非常簡單(作爲解決方法): 只需將任何鍵放入列表中,對其進行排序,然後使用元素作爲鍵訪問並打印字典即可按元素訪問它。