python排序字​​典的长度值 [英] python sorting dictionary by length of values

查看:117
本文介绍了python排序字​​典的长度值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经发现了许多线程来排序,如此处,但它似乎没有为我工作...



我有一个具有元组的列表字典。每个列表都有不同数量的元组。我想为每个列表包含多少个元组排序字典。

 >>> to_format 
> ;>> { 一:[(1,3),(1,4)], 二:[(1,2),(1,2),(1,3)],三:[(1,1)]}
>>对于some_sort(to_format)中的键:
打印键,
>>>两个三

这是可能吗?

解决方案

 >>> d = {一:[(1,3),(1,4)],二:[(1,2),(1,2),(1,3)],三 (1,1)]} 
>>>对于k进行排序(d,key = lambda k:len(d [k]),reverse = True):
print k,


两个三

这也可以:

 >>> print''.join(sorted(d,key = lambda k:len(d [k]),reverse = True))
两个三


I have found many threads for sorting by values like here but it doesn't seem to be working for me...

I have a dictionary of lists that have tuples. Each list has a different amount of tuples. I want to sort the dictionary by how many tuples each list contain.

>>>to_format 
>>>{"one":[(1,3),(1,4)],"two":[(1,2),(1,2),(1,3)],"three":[(1,1)]}
>>>for key in some_sort(to_format):
       print key,
>>>two one three

Is this possible?

解决方案

>>> d = {"one": [(1,3),(1,4)], "two": [(1,2),(1,2),(1,3)], "three": [(1,1)]}
>>> for k in sorted(d, key=lambda k: len(d[k]), reverse=True):
        print k,


two one three

This also works:

>>> print ' '.join(sorted(d, key=lambda k: len(d[k]), reverse=True))
two one three

这篇关于python排序字​​典的长度值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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