2012-08-01 162 views
4

任何人都有任何想法如何按關鍵字長度排序此字典?字典按關鍵字長度排序

{ 
    '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。

+4

字典無序;你想只顯示字典嗎?以排序的順序循環鍵?請具體說明您的預期產出。 – 2012-08-01 06:35:48

+0

我想通過密鑰長度對字典進行排序...不用於顯示,在應用程序中進一步使用 – badc0re 2012-08-01 06:38:22

+0

字典無法排序。 – 2012-08-01 06:54:27

回答

3
>>> 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'}])]) 
+0

我使用python的舊版本=> 2.6 – badc0re 2012-08-01 06:40:28

+0

@ badc0re您可以搜索'OrderedDict recipe for Python 2.6' – jamylak 2012-08-01 06:41:43

4
newlist = yourdict.items() 
sortedlist = sorted(newlist, key=lambda s: len(s[0])) 

會給你元組它們由原始關鍵的長度排序的新列表

+1

不需要它...'list.sort'是一個inplace函數並返回'None'。將它更改爲'sorted(newlist,key = lambda s:len(s [0]))' – jamylak 2012-08-01 06:57:03

+0

謝謝@jamylak,編輯 – Aesthete 2012-08-01 07:00:47

+0

'newlist'中的所有元組都有長度爲2的字符串。您需要'key = lambda s:len (s [0])' – 2012-08-01 07:04:34