使用词典值排序列表(最大值到最小值)

时间:2016-10-19 22:23:41

标签: python arrays sorting dictionary

我有一个包含大量字典的数组。但是我想以一种方式对字典进行排序,其中我对字典中的特定键具有最大值。 例如,我有一个看起来像这样的列表

[
    {
        "num_gurus": 40,
        "id": 119749,
        "code": null,
        "name": "ART 198P",
        "short_name": "ART 198P",
        "title": "Directed Group Study",
        "department_long": null,
        "full_name": "Directed Group Study",
        "department_short": "ART"
    },
    {
        "num_gurus": 3,
        "id": 119825,
        "code": null,
        "name": "ASAMST 198P",
        "short_name": "ASAMST 198P",
        "title": "Supervised Group Study",
        "department_long": null,
        "full_name": "Supervised Group Study",
        "department_short": "ASAMST"
    },
    {
        "num_gurus": 200,
        "id": 119904,
        "code": null,
        "name": "AST 636",
        "short_name": "AST 636",
        "title": "Baudelaire: Art Poetry Modernity",
        "department_long": null,
        "full_name": "Baudelaire: Art Poetry Modernity",
        "department_short": "AST"
    }
]

我希望我的输出对我的词典进行排序,其中键属性'num_gurus'的值最大为最小值。预期的产出将是。

[
    {
        "num_gurus": 200,
        "id": 119904,
        "code": null,
        "name": "AST 636",
        "short_name": "AST 636",
        "title": "Baudelaire: Art Poetry Modernity",
        "department_long": null,
        "full_name": "Baudelaire: Art Poetry Modernity",
        "department_short": "AST"
    }
    {
        "num_gurus": 40,
        "id": 119749,
        "code": null,
        "name": "ART 198P",
        "short_name": "ART 198P",
        "title": "Directed Group Study",
        "department_long": null,
        "full_name": "Directed Group Study",
        "department_short": "ART"
    },
    {
        "num_gurus": 3,
        "id": 119825,
        "code": null,
        "name": "ASAMST 198P",
        "short_name": "ASAMST 198P",
        "title": "Supervised Group Study",
        "department_long": null,
        "full_name": "Supervised Group Study",
        "department_short": "ASAMST"
    }

]

到目前为止我已尝试过这个

    for items in load_as_json:
            for key, val in sorted(items['num_gurus'].iteritems(), key=lambda (k,v): (v,k), reverse=True):
                print key,val

This throws me error and doesn't do what I actually want to.
This is the error I got.
  File "utils.py", line 61, in GetPopularCoursesBasedOnGurus
    for key, val in sorted(str(items['num_gurus']).iteritems(), key=lambda (k,v): (v,k)):
AttributeError: 'str' object has no attribute 'iteritems'

2 个答案:

答案 0 :(得分:2)

试试这个:

my_list.sort(key=lambda my_dict: my_dict["num_gurus"], reverse=True)

这样做基本上是两件事:

  • key paramater需要一个匿名函数(python中的lambda)然后排序 原始列表值由返回的值返回 lambda函数。 lambda my_dict: my_dict["num_gurus"]返回" num_gurus"每个字典中的项目因此列表按这些值排序。
  • reverse=True默认排序功能从最小到最大排序,因此 这简单地反转了

我也发现这非常不安全"因为你没有保证" num_gurus"你的字典中的键,或字典作为键值,因此我亲自用一些异常处理程序包装它:try \ except

在这里阅读更多内容:https://docs.python.org/2.7/tutorial/errors.html,记住更好的安全而不是抱歉!

答案 1 :(得分:1)

对于 将已排序的列表存储为新列表 ,您可以使用sorted()执行此操作:

sorted(my_list, key=lambda x: x['num_gurus'], reverse=True)
# returns sorted list

其中my_listlistdict个对象。

否则,如果您想 对原始列表的内容进行排序 ,即my_list,请使用list.sort()作为:

my_list.sort(key=lambda x: x["num_gurus"], reverse=True)
# sorts the original list

检查文档:How to do Sorting in list