根据Python中词典的值进行排名

时间:2020-07-27 19:58:07

标签: python

我将以下信息构造为词典列表:

tax_calc = [
{
    "comp":"Gle LTD",
    "tax":120
},
{
    "comp":"Pep LDT",
    "tax":1522
},
{
    "comp":"Leg Inc",
    "tax":246
},
{
    "comp":"Nen Inc",
    "tax":300
}]

获得最高和最低退税(税)的公司名称的最佳方法是什么?

谢谢!

1 个答案:

答案 0 :(得分:0)

根据@hiro主角的评论,您需要做的是

    {
    "name": "predis/predis",
    "version": "v1.1.1",
    "version_normalized": "1.1.1.0",
    "source": {
        "type": "git",
        "url": "https://github.com/nrk/predis.git",
        "reference": "f0210e38881631afeafb56ab43405a92cafd9fd1"
    },
    "dist": {
        "type": "zip",
        "url": "https://api.github.com/repos/nrk/predis/zipball/f0210e38881631afeafb56ab43405a92cafd9fd1",
        "reference": "f0210e38881631afeafb56ab43405a92cafd9fd1",
        "shasum": ""
    },
    "require": {
        "php": ">=5.3.9"
    },
    "require-dev": {
        "phpunit/phpunit": "~4.8"
    },
    "suggest": {
        "ext-curl": "Allows access to Webdis when paired with phpiredis",
        "ext-phpiredis": "Allows faster serialization and deserialization of the Redis protocol"
    },
    "time": "2016-06-16T16:22:20+00:00",
    "type": "library",
    "installation-source": "dist",
    "autoload": {
        "psr-4": {
            "Predis\\": "src/"
        }
    },
    "notification-url": "https://packagist.org/downloads/",
    "license": [
        "MIT"
    ],
    "authors": [
        {
            "name": "Daniele Alessandri",
            "email": "suppakilla@gmail.com",
            "homepage": "http://clorophilla.net"
        }
    ],
    "description": "Flexible and feature-complete Redis client for PHP and HHVM",
    "homepage": "http://github.com/nrk/predis",
    "keywords": [
        "nosql",
        "predis",
        "redis"
    ]
}

这将根据每个项目的税号对您的列表进行排序。

然后,您只需要获取第一个公司名称和最后一个公司名称

tax_calc = [
{
    "comp":"Gle LTD",
    "tax":120
},
{
    "comp":"Pep LDT",
    "tax":1522
},
{
    "comp":"Leg Inc",
    "tax":246
},
{
    "comp":"Nen Inc",
    "tax":300
}]

tax_calc.sort(key=lambda dct: dct["tax"])