2012-05-24 179 views
3

的價值,這是我的JSON:排序子類型的字典中的字典由副字典

{"data": [{"L": "Leinster"}, {"RN": "Roscommon"}, {"G": "Galway"}, {"LS": "Laois"}, {"LD": "Longford"}, {"OY": "Offaly"}, {"KK": "Kilkenny"}, {"SO": "Sligo"}, {"C": "Connaught"}, {"CO": "Cork"}, {"M": "Munster"}, {"WD": "Waterford"}, {"CE": "Clare"}]} 

我要進行排序,以便在字典將由類似的價值「ABC」被下令:

{"data": [ {"CE": "Clare"},{"C": "Connaught"}, {"CO": "Cork"}, {"G": "Galway"},{"KK": "Kilkenny"}, {"LS": "Laois"},{"L": "Leinster"}, {"LD": "Longford"}, {"M": "Munster"},{"OY": "Offaly"}, {"RN": "Roscommon"}, {"SO": "Sligo"}, {"WD": "Waterford"} ]} 

有些想法嗎?試過這Python: sort this dictionary (dict in dict) 但這是排序值。而不是字典中的值。

謝謝!

回答

4

對於Python 2

>>> d = {'data': [{'L': 'Leinster'}, {'RN': 'Roscommon'}, {'G': 'Galway'}, {'LS': 'Laois'}, {'LD': 'Longford'}, {'OY': 'Offaly'}, {'KK': 'Kilkenny'}, {'SO': 'Sligo'}, {'C': 'Connaught'}, {'CO': 'Cork'}, {'M': 'Munster'}, {'WD': 'Waterford'}, {'CE': 'Clare'}]} 
>>> dict((k,sorted(v,key=dict.values)) for k,v in d.iteritems()) 
{'data': [{'CE': 'Clare'}, {'C': 'Connaught'}, {'CO': 'Cork'}, {'G': 'Galway'}, {'KK': 'Kilkenny'}, {'LS': 'Laois'}, {'L': 'Leinster'}, {'LD': 'Longford'}, {'M': 'Munster'}, {'OY': 'Offaly'}, {'RN': 'Roscommon'}, {'SO': 'Sligo'}, {'WD': 'Waterford'}]} 

這裏是一個Python 3的解決方案,如果有,因爲我不喜歡太多的東西比這更好這將是偉大知道。

>>> {k:sorted(v,key=lambda x: tuple(x.values())) for k,v in d.items()} 
{'data': [{'CE': 'Clare'}, {'C': 'Connaught'}, {'CO': 'Cork'}, {'G': 'Galway'}, {'KK': 'Kilkenny'}, {'LS': 'Laois'}, {'L': 'Leinster'}, {'LD': 'Longford'}, {'M': 'Munster'}, {'OY': 'Offaly'}, {'RN': 'Roscommon'}, {'SO': 'Sligo'}, {'WD': 'Waterford'}]} 
+1

+1,不會發布我的現在,即使它有點不同它不是最好的:^) –

+0

太棒了!謝謝!! – eligro

+0

@eligro沒問題:) – jamylak

1

或可能是你可以簡單地做

更新的排序值

In [41]: r={"data": [{"L": "Leinster"}, {"RN": "Roscommon"}, {"G": "Galway"}, {"LS": "Laois"}, {"LD": "Longford"}, {"OY": "Offaly"}, {"KK": "Kilkenny"}, {"SO": "Sligo"}, {"C": "Connaught"}, {"CO": "Cork"}, {"M": "Munster"}, {"WD": "Waterford"}, {"CE": "Clare"}]} 

In [42]: for v in r.itervalues(): 
....:  v.sort(key=dict.values) 
....: 

In [43]: r 
Out[43]: 
{'data': [{'CE': 'Clare'}, 
     {'C': 'Connaught'}, 
     {'CO': 'Cork'}, 
     {'G': 'Galway'}, 
     {'KK': 'Kilkenny'}, 
     {'LS': 'Laois'}, 
     {'L': 'Leinster'}, 
     {'LD': 'Longford'}, 
     {'M': 'Munster'}, 
     {'OY': 'Offaly'}, 
     {'RN': 'Roscommon'}, 
     {'SO': 'Sligo'}, 
     {'WD': 'Waterford'}]} 
+1

可以通過用'v.sort(key = dict.values)'替換'v.sort()'來解決。 – srgerg

+0

kk ..如果你需要根據內部字典的值進行排序 – gsagrawal

+0

我喜歡這個,因爲它對列表進行排序而不是創建一個新的字典,它比我的hacky解決方案更優雅。 – srgerg

1

另一種解決方案,因爲越多越開心!

>>> r={"data": [{"L": "Leinster"}, {"RN": "Roscommon"}, {"G": "Galway"}, {"LS": "Laois"}, {"LD": "Longford"}, {"OY": "Offaly"}, {"KK": "Kilkenny"}, {"SO": "Sligo"}, {"C": "Connaught"}, {"CO": "Cork"}, {"M": "Munster"}, {"WD": "Waterford"}, {"CE": "Clare"}]} 
>>> 
>>> map(functools.partial(list.sort, key=dict.values), r.itervalues()) 
[None] 
>>> 
>>> r 
{'data': [{'CE': 'Clare'}, {'C': 'Connaught'}, {'CO': 'Cork'}, {'G': 'Galway'}, {'KK': 'Kilkenny'}, {'LS': 'Laois'}, {'L': 'Leinster'}, {'LD': 'Longford'}, {'M': 'Munster'}, {'OY': 'Offaly'}, {'RN': 'Roscommon'}, {'SO': 'Sligo'}, {'WD': 'Waterford'}]} 
+0

太醜陋和醜陋:D – jamylak

+0

是的,這是一個黑客... – srgerg