Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
268 views
in Technique[技术] by (71.8m points)

Selecting only max value in python list of dicts

I have a list of dicts as follows:

[{"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}]}
           ]
}]

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}}
            ]
 }]
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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))

The output:

[
    {
        "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
                }
            }
        ]
    }
]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...