在python列表中只选择最大值 [英] Selecting only max value in python list of dicts

查看:345
本文介绍了在python列表中只选择最大值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表如下:

[{"server":"8.8.8.8", 
  "domains":[{"google.com":[{"time":15, "serial":14}, {"time":78, "serial":14}]},
             {"intuit.com":[{"time":20, "serial":23}, {"time":91, "serial":18}]}
            ]
},
{"server":"8.8.4.4", 
 "domains":[{"google.com":[{"time":19, "serial":45}, {"time":92, "serial":76}]},
            {"intuit.com":[{"time":45, "serial":89}, {"time":93, "serial":74}]}
           ]
},
{"server":"206.67.222.222", 
 "domains":[{"google.com":[{"time":98, "serial":76}, {"time":64, "serial":54}]},
            {"intuit.com":[{"time":43, "serial":21}, {"time":65, "serial":59}]}
           ]
}]

我将如何创建一个结构,其中我只选择每个域的dict,具有最大序列号,当我有相同的序列号时,选择最大时间,以便我留下以下内容:

How would I go about creating a structure where I select only the dict for each domain with the max serial number and when I have the same serial number, select the max time so that I am left with the following:

[{"server":"8.8.8.8", 
  "domains":[{"google.com":{"time":78, "serial":14}},
             {"intuit.com":{"time":20, "serial":23}}
            ]
 },
 {"server":"8.8.4.4", 
  "domains":[{"google.com":{"time":92, "serial":76}},
             {"intuit.com":{"time":45, "serial":89}}
            ]
 },
 {"server":"206.67.222.222", 
  "domains":[{"google.com":{"time":98, "serial":76}},
             {"intuit.com":{"time":65, "serial":59}}
            ]
 }]


推荐答案

内置 max() function:

The solution using built-in max() function:

import json

# l is your initial list of dicts
for item in l:
    for d in item['domains']:
        for k, v in d.items():
            # whether `serial` numbers are unique 
            has_uniq_serial = len(set([i['serial'] for i in v])) > 1
            d[k] = max(v, key=lambda o: o['serial']) if has_uniq_serial else max(v, key=lambda o: o['time'])

# `json.dumps` used for pretty printing of nested dicts
print(json.dumps(l, indent=4))

输出:

[
    {
        "server": "8.8.8.8",
        "domains": [
            {
                "google.com": {
                    "serial": 14,
                    "time": 78
                }
            },
            {
                "intuit.com": {
                    "serial": 23,
                    "time": 20
                }
            }
        ]
    },
    {
        "server": "8.8.4.4",
        "domains": [
            {
                "google.com": {
                    "serial": 76,
                    "time": 92
                }
            },
            {
                "intuit.com": {
                    "serial": 89,
                    "time": 45
                }
            }
        ]
    },
    {
        "server": "206.67.222.222",
        "domains": [
            {
                "google.com": {
                    "serial": 76,
                    "time": 98
                }
            },
            {
                "intuit.com": {
                    "serial": 59,
                    "time": 65
                }
            }
        ]
    }
]

这篇关于在python列表中只选择最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆