Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/335.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 按键长度排序的字典_Python_Sorting_Dictionary_Ordereddictionary - Fatal编程技术网

Python 按键长度排序的字典

Python 按键长度排序的字典,python,sorting,dictionary,ordereddictionary,Python,Sorting,Dictionary,Ordereddictionary,有人知道如何按键长对这本词典进行排序吗 { 'http://ccc.com/viewvc/' : [{'type': 'web-servers', 'app': 'Apache', 'ver': '2.2.14'}, {'type': 'operating-systems', 'app': 'Ubuntu', 'ver': None}], 'http://bbb.com/' : [{'type': 'web-servers', 'app': 'Apache', 'ver'

有人知道如何按键长对这本词典进行排序吗

{
    'http://ccc.com/viewvc/' : [{'type': 'web-servers', 'app': 'Apache', 'ver': '2.2.14'}, {'type': 'operating-systems', 'app': 'Ubuntu', 'ver': None}],    
    'http://bbb.com/' : [{'type': 'web-servers', 'app': 'Apache', 'ver': '2.2.22'}, {'type': 'programming-languages', 'app': 'PHP', 'ver': '5.3.10'}, {'type': 'cms', 'app': 'Drupal', 'ver': None}, {'type': 'operating-systems', 'app': 'Ubuntu', 'ver': None}, {'type': 'javascript-frameworks', 'app': 'jQuery', 'ver': None}, {'type': 'captchas', 'app': 'Mollom', 'ver': None}]
}
预期产出:

{
    'http://bbb.com/' : [{'type': 'web-servers', 'app': 'Apache', 'ver': '2.2.22'}, {'type': 'programming-languages', 'app': 'PHP', 'ver': '5.3.10'}, {'type': 'cms', 'app': 'Drupal', 'ver': None}, {'type': 'operating-systems', 'app': 'Ubuntu', 'ver': None}, {'type': 'javascript-frameworks', 'app': 'jQuery', 'ver': None}, {'type': 'captchas', 'app': 'Mollom', 'ver': None}]
    'http://ccc.com/viewvc/' : [{'type': 'web-servers', 'app': 'Apache', 'ver': '2.2.14'}, {'type': 'operating-systems', 'app': 'Ubuntu', 'ver': None}],    

}
我正在使用Python 2.6。

Python v2.7+ 早期Python版本 看


将为您提供一个新的元组列表,元组按原始键的长度排序。

字典是无序的;您想只显示字典吗?按排序顺序循环键?请具体说明您的预期输出。我想按键长度对字典进行排序…不是为了显示,而是为了在应用程序中进一步使用。字典无法排序。我使用的是较旧版本的python=>2。6@badc0re您可以搜索“OrderedDict配方Python2.6”不,它不会
list.sort
是一个内置函数,返回
None
。将其更改为
sorted(newlist,key=lambda s:len(s[0]))
newlist中的所有元组的长度均为2。你需要
key=lambda s:len(s[0])
Err我知道我这样做是有原因的。一定是回家时间了!谢谢@DavidRobinson
>>> from collections import OrderedDict
>>> d = {
    'http://ccc.com/viewvc/' : [{'type': 'web-servers', 'app': 'Apache', 'ver': '2.2.14'}, {'type': 'operating-systems', 'app': 'Ubuntu', 'ver': None}],    
    'http://bbb.com/' : [{'type': 'web-servers', 'app': 'Apache', 'ver': '2.2.22'}, {'type': 'programming-languages', 'app': 'PHP', 'ver': '5.3.10'}, {'type': 'cms', 'app': 'Drupal', 'ver': None}, {'type': 'operating-systems', 'app': 'Ubuntu', 'ver': None}, {'type': 'javascript-frameworks', 'app': 'jQuery', 'ver': None}, {'type': 'captchas', 'app': 'Mollom', 'ver': None}]
}
>>> OrderedDict(sorted(d.iteritems(), key=lambda x: len(x[0])))
OrderedDict([('http://bbb.com/', [{'ver': '2.2.22', 'app': 'Apache', 'type': 'web-servers'}, {'ver': '5.3.10', 'app': 'PHP', 'type': 'programming-languages'}, {'ver': None, 'app': 'Drupal', 'type': 'cms'}, {'ver': None, 'app': 'Ubuntu', 'type': 'operating-systems'}, {'ver': None, 'app': 'jQuery', 'type': 'javascript-frameworks'}, {'ver': None, 'app': 'Mollom', 'type': 'captchas'}]), ('http://ccc.com/viewvc/', [{'ver': '2.2.14', 'app': 'Apache', 'type': 'web-servers'}, {'ver': None, 'app': 'Ubuntu', 'type': 'operating-systems'}])])
newlist = yourdict.items()
sortedlist = sorted(newlist, key=lambda s: len(s[0]))