Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Dict的排序值_Python_Sorting_Dictionary - Fatal编程技术网

Python Dict的排序值

Python Dict的排序值,python,sorting,dictionary,Python,Sorting,Dictionary,收益率: for y in item: print '%s %s %s' % (item[y]['light'], item[y]['time'], item[y]['name']) 我想根据第一个键“light”对项目进行排序 非常感谢您的帮助。我认为您应该将其转换为列表,然后: 10.38002 2.95000 earth 10.38000 12.04000 earth 10.28865 0.09789 earth 9.90000 12.50000 earth 9.77063 3.

收益率:

for y in item:
    print '%s %s %s' % (item[y]['light'], item[y]['time'], item[y]['name'])
我想根据第一个键“light”对项目进行排序


非常感谢您的帮助。

我认为您应该将其转换为列表,然后:

10.38002 2.95000 earth
10.38000 12.04000 earth
10.28865 0.09789 earth
9.90000 12.50000 earth
9.77063 3.51299 earth
9.77000 13.40000 earth
10.38000000 1.26523000 moon
10.36000000 65.80305000 moon
10.31000000 1.72639000 moon
10.30000000 2.00000000 moon
10.33726 0.09833000 jupiter
10.30938 0.16622000 jupiter
10.24001 0.34554000 jupiter
10.24000 11.82214802 jupiter
10.23389 0.10840000 jupiter
10.20629 0.17732000 jupiter

您的意思是希望按(键“light”)的值进行排序。你在StackOverflow中搜索过字典排序吗?已经有很多好的答案了,我试过很多答案。我现在正在尝试转换为列表,然后执行thikonom的[Constructional!]回答。
项是一个
dict
,它没有
排序
成员。我认为它是一个列表:x=[{'light':1,'time':2,'name':'name'}工作得很好,谢谢!不过,对于初学者的问题,我很抱歉!
  item.sort(key=lambda x: x['light'])
res = sorted(item.values(), key = lambda x: x['light'] )
for x in res: 
    print '%s %s %s' % (x['light'], x['time'], x['name'])