使用多个值对字典进行排序

时间:2015-10-29 16:40:19

标签: python sorting dictionary

我正在尝试对字典进行排序,我查看了很多示例,但它们都显示了非常简单的字典。我看过orderedDict,itemgetter,lambda等但是我看不出如何按位置,homepoints或者远点对这本词典进行排序:

{
    u'SwanseaCityFC': {
        'position': 12,
        'awayPoints': 7,
        'homePoints': 8
    },
    u'TottenhamHotspurFC': {
        'position': 6,
        'awayPoints': 11,
        'homePoints': 9
    },
    u'StokeCityFC': {
        'position': 14,
        'awayPoints': 9,
        'homePoints': 4
    },
    u'AstonVillaFC': {
        'position': 20,
        'awayPoints': 4,
        'homePoints': 1
    },
    u'ManchesterCityFC': {
        'position': 1,
        'awayPoints': 10,
        'homePoints': 12
    },
    u'AFCBournemouth': {
        'position': 17,
        'awayPoints': 5,
        'homePoints': 5
    },
    u'CrystalPalaceFC': {
        'position': 7,
        'awayPoints': 9,
        'homePoints': 6
    },
    u'NewcastleUnitedFC': {
        'position': 19,
        'awayPoints': 3,
        'homePoints': 5
    },
    u'NorwichCityFC': {
        'position': 16,
        'awayPoints': 6,
        'homePoints': 4
    },
    u'WatfordFC': {
        'position': 13,
        'awayPoints': 10,
        'homePoints': 5
    },
    u'ManchesterUnitedFC': {
        'position': 4,
        'awayPoints': 11,
        'homePoints': 11
    },
    u'LeicesterCityFC': {
        'position': 5,
        'awayPoints': 10,
        'homePoints': 10
    },
    u'ChelseaFC': {
        'position': 15,
        'awayPoints': 5,
        'homePoints': 7
    },
    u'WestHamUnitedFC': {
        'position': 3,
        'awayPoints': 14,
        'homePoints': 7
    },
    u'SunderlandAFC': {
        'position': 18,
        'awayPoints': 3,
        'homePoints': 5
    },
    u'SouthamptonFC': {
        'position': 8,
        'awayPoints': 8,
        'homePoints': 7
    },
    u'EvertonFC': {
        'position': 11,
        'awayPoints': 10,
        'homePoints': 5
    },
    u'LiverpoolFC': {
        'position': 9,
        'awayPoints': 8,
        'homePoints': 8
    },
    u'ArsenalFC': {
        'position': 2,
        'awayPoints': 13,
        'homePoints': 10
    },
    u'WestBromwichAlbionFC': {
        'position': 10,
        'awayPoints': 11,
        'homePoints': 4
    }
}

任何帮助表示赞赏!

2 个答案:

答案 0 :(得分:0)

转到collections文档,您可以看到OrderedDict实施说明https://docs.python.org/3/library/collections.html#collections.OrderedDict

有如下例子:

>>> # dictionary sorted by value
>>> OrderedDict(sorted(d.items(), key=lambda t: t[1]))
OrderedDict([('pear', 1), ('orange', 2), ('banana', 3), ('apple', 4)])

然后您可以扩展到您自己的用法:

>>> d = {'banana':{'key':3},'apple':{'key':4}, 'pear': {'key':1}, 'orange': {'key':2}}
>>> OrderedDict(sorted(d.items(), key=lambda t: t[1]['key']))
OrderedDict([('pear', {'key': 1}), ('orange', {'key': 2}), ('banana', {'key': 3}), ('apple', {'key': 4})])

答案 1 :(得分:0)

Sort dictionary by multiple values为起点,这是您可以进行排序的示例。

dict = { ... your dictionary ... }
# sort by position
for item in sorted(dict, key = lambda k: dict[k]['position']):
    print(dict[item])

# sort by awayPoints, then position; note the lambda uses a tuple
for item in sorted(dict, key = lambda k: (dict[k]['awayPoints'], dict[k]['position'])):
    print(mylist[item])