[英]sorting nested keys in a dictionary
dict_1 = {
(40, 615): {1: ["03/02/2013"]},
(40, 558): {1: ["03/20/2013"]},
(40, 501): {1: ["04/03/2013"]},
(40, 342): {1: ["04/04/2013"]},
(40, 248): {1: ["04/08/2013"]},
(40, 88): {1: ["04/12/2013"]},
(40, 584): {8: ["06/21/2013"]},
(40, 480): {2: ["04/12/2013"]},
(40, 321): {2: ["04/12/2013"]},
(40, 121): {2: ["04/13/2013"]},
(40, 563): {3: ["04/15/2013"]},
(40, 404): {3: ["04/24/2013"]},
(40, 245): {3: ["04/26/2013"]},
(40, 141): {3: ["04/26/2013"]},
(40, 578): {4: ["04/29/2013"]},
(40, 474): {4: ["04/30/2013"]},
(40, 370): {4: ["04/30/2013"]},
(40, 201): {7: ["06/20/2013"]},
(64, 643): {5: ["05/03/2013"]},
(40, 484): {5: ["05/08/2013"]},
(40, 287): {5: ["05/31/2013"]},
(40, 171): {5: ["05/31/2013"]},
(40, 660): {6: ["06/03/2013"]},
(40, 544): {6: ["06/04/2013"]},
(40, 440): {6: ["06/12/2013"]},
(40, 281): {6: ["06/12/2013"]},
(40, 177): {6: ["06/12/2013"]},
(40, 619): {7: ["06/13/2013"]},
(40, 515): {7: ["06/14/2013"]},
(40, 411): {7: ["06/18/2013"]},
(40, 295): {7: ["06/19/2013"]},
(40, 97): {7: ["06/21/2013"]},
}
I want to sort this dictionary on nested keys.我想在嵌套键上对这本字典进行排序。
Here is what I tried这是我尝试过的
def sort_dictionary(*new_dict):
for unpack in new_dict:
for key, value in sorted(unpack .items()):
return key,':',value
res = sorted(dict_1 .items(), key = lambda x: sort_dictionary(x[1]))
Of course, it is not giving me what I am expecting, please tell me if there is an easier way?当然,它并没有给我我所期望的,请告诉我是否有更简单的方法?
def sort_dict(d):
return dict(sorted(d.items(), key = lambda x: tuple(x[1].keys())[0]))
dict_1 = {
(40, 615): {1: ["03/02/2013"]},
(40, 558): {1: ["03/20/2013"]},
(40, 501): {1: ["04/03/2013"]},
(40, 342): {1: ["04/04/2013"]},
(40, 248): {1: ["04/08/2013"]},
(40, 88): {1: ["04/12/2013"]},
(40, 584): {8: ["06/21/2013"]},
(40, 480): {2: ["04/12/2013"]},
(40, 321): {2: ["04/12/2013"]},
(40, 121): {2: ["04/13/2013"]},
(40, 563): {3: ["04/15/2013"]},
(40, 404): {3: ["04/24/2013"]},
(40, 245): {3: ["04/26/2013"]},
(40, 141): {3: ["04/26/2013"]},
(40, 578): {4: ["04/29/2013"]},
(40, 474): {4: ["04/30/2013"]},
(40, 370): {4: ["04/30/2013"]},
(40, 201): {7: ["06/20/2013"]},
(64, 643): {5: ["05/03/2013"]},
(40, 484): {5: ["05/08/2013"]},
(40, 287): {5: ["05/31/2013"]},
(40, 171): {5: ["05/31/2013"]},
(40, 660): {6: ["06/03/2013"]},
(40, 544): {6: ["06/04/2013"]},
(40, 440): {6: ["06/12/2013"]},
(40, 281): {6: ["06/12/2013"]},
(40, 177): {6: ["06/12/2013"]},
(40, 619): {7: ["06/13/2013"]},
(40, 515): {7: ["06/14/2013"]},
(40, 411): {7: ["06/18/2013"]},
(40, 295): {7: ["06/19/2013"]},
(40, 97): {7: ["06/21/2013"]},
}
print(sort_dict(dict_1))
Output:输出:
{
(40, 615): {1: ["03/02/2013"]},
(40, 558): {1: ["03/20/2013"]},
(40, 501): {1: ["04/03/2013"]},
(40, 342): {1: ["04/04/2013"]},
(40, 248): {1: ["04/08/2013"]},
(40, 88): {1: ["04/12/2013"]},
(40, 480): {2: ["04/12/2013"]},
(40, 321): {2: ["04/12/2013"]},
(40, 121): {2: ["04/13/2013"]},
(40, 563): {3: ["04/15/2013"]},
(40, 404): {3: ["04/24/2013"]},
(40, 245): {3: ["04/26/2013"]},
(40, 141): {3: ["04/26/2013"]},
(40, 578): {4: ["04/29/2013"]},
(40, 474): {4: ["04/30/2013"]},
(40, 370): {4: ["04/30/2013"]},
(64, 643): {5: ["05/03/2013"]},
(40, 484): {5: ["05/08/2013"]},
(40, 287): {5: ["05/31/2013"]},
(40, 171): {5: ["05/31/2013"]},
(40, 660): {6: ["06/03/2013"]},
(40, 544): {6: ["06/04/2013"]},
(40, 440): {6: ["06/12/2013"]},
(40, 281): {6: ["06/12/2013"]},
(40, 177): {6: ["06/12/2013"]},
(40, 201): {7: ["06/20/2013"]},
(40, 619): {7: ["06/13/2013"]},
(40, 515): {7: ["06/14/2013"]},
(40, 411): {7: ["06/18/2013"]},
(40, 295): {7: ["06/19/2013"]},
(40, 97): {7: ["06/21/2013"]},
(40, 584): {8: ["06/21/2013"]},
}
def sort_dict(d):
return sorted(d.items(), key = lambda x: tuple(x[1].keys())[0])
sorted_dictionary = sort_dict(dict_1)
This code works great!这段代码效果很好! But then I had to write some extra lines of code to convert sorted_dictionary
from a list of tuples to a nested dictionary.但是后来我不得不编写一些额外的代码行来将sorted_dictionary
从元组列表转换为嵌套字典。
new_dict = {}
for item in sorted_dictionary :
new_dict[item[0]] = item[1]
print(new_dict)
{(40, 615): {1: ['03/02/2013']},
(40, 558): {1: ['03/20/2013']},
(40, 501): {1: ['04/03/2013']},
(40, 342): {1: ['04/04/2013']},
(40, 248): {1: ['04/08/2013']},
(40, 88): {1: ['04/12/2013']},
(40, 480): {2: ['04/12/2013']},
(40, 321): {2: ['04/12/2013']},
(40, 121): {2: ['04/13/2013']},
(40, 563): {3: ['04/15/2013']},
(40, 404): {3: ['04/24/2013']},
(40, 245): {3: ['04/26/2013']},
(40, 141): {3: ['04/26/2013']},
(40, 578): {4: ['04/29/2013']},
(40, 474): {4: ['04/30/2013']},
(40, 370): {4: ['04/30/2013']},
(64, 643): {5: ['05/03/2013']},
(40, 484): {5: ['05/08/2013']},
(40, 287): {5: ['05/31/2013']},
(40, 171): {5: ['05/31/2013']},
(40, 660): {6: ['06/03/2013']},
(40, 544): {6: ['06/04/2013']},
(40, 440): {6: ['06/12/2013']},
(40, 281): {6: ['06/12/2013']},
(40, 177): {6: ['06/12/2013']},
(40, 201): {7: ['06/20/2013']},
(40, 619): {7: ['06/13/2013']},
(40, 515): {7: ['06/14/2013']},
(40, 411): {7: ['06/18/2013']},
(40, 295): {7: ['06/19/2013']},
(40, 97): {7: ['06/21/2013']},
(40, 584): {8: ['06/21/2013']}}
Is there a better way without the extra lines of code?没有额外的代码行,有没有更好的方法?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.