Sorting 对字典的输出进行排序

Sorting 对字典的输出进行排序,sorting,dictionary,python-3.x,formatting,Sorting,Dictionary,Python 3.x,Formatting,好的,我有下面的行星词典,每个行星都有自己的包含其规格的词典: 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, 'Atmospher

好的,我有下面的行星词典,每个行星都有自己的包含其规格的词典:

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中运行它时,行星名称和它的规格都打印在它下面,我只是不知道如何格式化它,以便在堆栈上以这种方式显示)

i、 e:


我尝试过使用
sorted()
,但这只是按字母顺序排列。有什么建议吗?

这是不可能的。Python
词典
不会对其打印顺序做出任何承诺。

这非常简单(作为一种解决方法):
只需将任意键放入列表中,对其进行排序,然后将其作为键逐个元素访问并打印词典。

您需要什么顺序?你所说的“词典中出现的顺序”是什么意思?字典不会以任何顺序保存元素。你不能这样做,字典不是为了顺序,它们主要是为了映射和检索。您可以做的是构建一个元组列表(x,y),其中x是键,y是值。您可以使用循环按该顺序打印出键和值的元组列表,然后将该列表传递到
dict()
,然后按该顺序打印后您将拥有字典。对于您自己的实现:啊,太糟糕了,我想您知道的越多。感谢您让我知道您可能会使用
collections.OrderedDict
。返回按其中一个属性排序的列表非常简单。例如,
sorted(d.items(),key=lambda(k,v):v[“离太阳的距离])
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