简体   繁体   English

按标签对嵌套的python字典进行排序

[英]Sorting nested python dictionary by label

I'm trying to find a smart way to sort the following data structure by std: 我正在尝试找到一种通过std对以下数据结构进行排序的聪明方法:

{'4555':{'std':5656, 'var': 5664}, '5667':{'std':5656, 'var': 5664}}

Ideally like to have a sorted dictionary (bad I know), or a list of sorted tuples, but I don't know how to get the 'std' part in my lambda expression. 理想情况下,我喜欢有一个排序的字典(我知道不好)或一个排序的元组列表,但是我不知道如何在我的lambda表达式中获得'std'部分。 I'm trying the following, but how should I get at the 'stdev' bit in a smart manner? 我正在尝试以下操作,但是如何以一种聪明的方式掌握“ stdev”呢? Which I want to go give a list of tuples (each tuple contains index such as [(4555, 5656), (5667, 5656)] . 我想给出一个元组列表(每个元组包含诸如[(4555, 5656), (5667, 5656)]索引。

sorted_list = sorted(sd_dict.items(), key=lambda x:x['std'])

Since sd_dict.items() returns a list of tuples, you no longer can access the elements as if it was a dictionary in the key function. 由于sd_dict.items()返回一个元组列表,因此您不再可以像访问key函数中的字典一样访问元素。 Instead, the key function gets a two-element tuple with the first element being the key and the second element being the value. 相反, key函数获得一个由两个元素组成的元组,第一个元素是键,第二个元素是值。 So to get the std value, you need to access it like this: 因此,要获取std值,您需要像这样访问它:

lambda x: x[1]['std']

But since in your example both values are identical, you don't actually change anything: 但是由于在您的示例中这两个值是相同的,因此您实际上无需进行任何更改:

>>> list(sorted(sd_dict.items(), key=lambda x: x[1]['std']))
[('5667', {'var': 5664, 'std': 5656}), ('4555', {'var': 5664, 'std': 5656})]

And if you just want a pair of the outer dictionary key and the std value, then you should use a list comprehension first to transform the dictionary values: 而且,如果您只想要一对外部字典键和std值,则应首先使用列表推导来转换字典值:

>>> lst = [(key, value['std']) for key, value in sd_dict.items()]
>>> lst.sort(key=lambda x: x[1])
>>> lst
[('5667', 5656), ('4555', 5656)]

Or maybe you want to include an int conversion, and also sort by the key too: 或者,也许您想包含一个int转换,并且还按键排序:

>>> lst = [(int(key), value['std']) for key, value in sd_dict.items()]
>>> lst.sort(key=lambda x: (x[1], x[0]))
>>> lst
[(4555, 5656), (5667, 5656)]

Each element in your input list is a tuple of (k, dictionary) items, so you need to index into that to get to the std key in the dictionary value: 输入列表中的每个元素都是(k, dictionary)项的元组,因此您需要索引到其中以获取字典值中的std键:

sorted(sd_dict.items(), key=lambda i: i[1]['std'])

If you wanted a tuple of just the key and the std value from the dictionary, you need to pick those out; 如果需要字典中的键和std值的元组,则需要选择它们; it doesn't matter if you do so before or after sorting, just adjust your sort key accordingly 排序之前还是之后都没关系,只需相应地调整排序键即可

sorted([(int(k), v['std']) for k, v in sd_dict.items()], key=lambda i: i[1])

or 要么

[(int(k), v['std']) for k, v in sorted(sd_dict.items()], key=lambda i: i[1]['std'])

However, extracting the std values just once instead of both for sorting and for extraction is going to be faster. 但是,只提取一次std值而不是同时进行排序和提取都将更快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM