在python中使用列表排序嵌套字典 [英] sorting a nested dictionary with lists in python

查看:938
本文介绍了在python中使用列表排序嵌套字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试排序包含列表的字典。例如,如果我有这个字典:

  a = {'q':{3:[4,2,7] },'a':{1:[5,45,11]},'e':{23:[11,45,2]}} 

我希望排序后的输出是:

  [(e, {23:[11,45,2}]),(a,{1:[5,45,11]}),(q,{3,[4,2,7]})] 

我实际上是排序反向,使用列表中的第一个项作为排序的键。



如果两个列表的第一个项目相同,如上所述,我按字母顺序排列与列表(主键)相关联的字符串。 p>

我不知道我是否可以使用字典获取元组的输出,因为我正在排序该字典中的列表。



我已经尝试过这个代码:

  sorted((x,b.items(),key =对于x,b在a.items())中的x:[1] [2])

提出了无效语法的错误,我无法弄清楚出了什么问题。

解决方案

我们来解决部分问题。你真的想排序列表 a.items()。所以:

 >>> to_sort = a.items()
>>> to_sort
[('q',{3:[4,2,7]}),('a',{1:[5,3,11]}),('e',{23: [11,45,2]})]

现在,对于列表中的每个元素,一个值的元组('q'等)和一个字典。大概每个字典只包含一个键,而您希望使用每个字典值的index-1元素作为主键。所以,第一个元素的关键是: to_sort [0] [1] .values()[0] [1] to_sort [ 0] [1] 给你字典 {3:[4,2,7]} .values )给你列表 [[4,2,7]] [0] [1] code>就可以让你 2 。次要排序键只是 to_sort [0]



所以我们得到:

 >>>排序(to_sort,key = lambda x:(x [1] .values()[0] [1],x [0])
[('q',{3:[4,2,7 ]}),('a',{1:[5,3,11]}),('e',{23:[11,45,2]})]

我们几乎在那里现在你只需要说明你想要的反转输出:

 >>>排序(to_sort,key = lambda x:(x [1] .values()[0] [1],x [0]),reverse = True 
[('e',{23:[11 ,45,2]}),('a',{1:[5,3,11]}),('q',{3:[4,2,7]})]

这是你想要的吗?



你可以做:

 >>>排序(a.items(),key = lambda x:(x [1] .values()[0] [1],x [0]),reverse = True)


I am trying to sort a dictionary containing lists. For example, if I have this dictionary:

a = {'q': {3: [4, 2, 7]}, 'a': {1: [5, 45, 11]}, 'e': {23: [11, 45, 2]}}

I want the output after sorting to be:

[(e, {23:[11,45,2}]), (a, {1:[5,45,11]}), (q,{3,[4,2,7]})] 

I am actually sorting in reverse, using the first item in the list as the key for the sort.

In the event that the first items of two lists are identical, like above, I sort for the string associated with the list (main key) in alphabetical order.

I am not sure if I can get the output of tuples with dictionary in it as I am sorting for that the list in that dictionary.

I have tried this code:

sorted((x,b.items(), key=lambda x:[1][2]) for x,b in a.items())

It raised an error for invalid syntax, and I can't figure out what's wrong.

解决方案

Let's break the problem in parts. You really want to sort the list a.items(). So:

>>> to_sort = a.items()
>>> to_sort
[('q', {3: [4, 2, 7]}), ('a', {1: [5, 3, 11]}), ('e', {23: [11, 45, 2]})]

Now, for each element in the list, you have a tuple of a value ('q' etc.) and a dictionary. Presumably, each dictionary contains only one key, and you want to use the index-1 element of each dictionary's value as the primary key. So, the key for the first element should be: to_sort[0][1].values()[0][1]: to_sort[0][1] gives you the dictionary {3: [4, 2, 7]}, .values() gives you the list [[4, 2, 7]], and [0][1] on that gives you 2. The secondary sort key is simply to_sort[0].

So we get:

>>> sorted(to_sort, key=lambda x: (x[1].values()[0][1], x[0]))
[('q', {3: [4, 2, 7]}), ('a', {1: [5, 3, 11]}), ('e', {23: [11, 45, 2]})]

We are almost there. Now you just need to tell sort that you want reversed output:

>>> sorted(to_sort, key=lambda x: (x[1].values()[0][1], x[0]), reverse=True)
[('e', {23: [11, 45, 2]}), ('a', {1: [5, 3, 11]}), ('q', {3: [4, 2, 7]})]

Is this what you want?

If you want a one-liner, you can do:

>>> sorted(a.items(), key=lambda x: (x[1].values()[0][1], x[0]), reverse=True)

这篇关于在python中使用列表排序嵌套字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆