Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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
                }
            }
        ]
    }
]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...