简体   繁体   中英

Python: filter list of dictionaries based on multiple key values

I am importing a log file with JSON-like dictionaries and attempting to: 1)only include logs where all three keys are included 2)create a new list of dictionaries that only have these key/values

log:

{"pytest_version": "7.1.e", "$report_type": "SessionStart"}
{"label": "test1", "outcome": "passed", "duration":0.0009}
{"label": "test2", "outcome": "passed", "duration":0.00019}

Script:

with open('log', 'r') as f:
    key_list = ['label', 'outcome']
    l = [json.loads(log) for log in logs]
    l2 = [{k:v for k, v in d.items() if k in key_list} for d in l]
    print(l2)

Output

 [{},
 {'label': 'test1', 'outcome': 'passed'},
 {'label': 'test2', 'outcome': 'passed'}]

Why is there an empty dictionary in the results? And how could it be modified to only include results if the key has a value (eg exclude 'outcome':'' )

The first empty dictionary {} is there because {"pytest_version": "7.1.e", "$report_type": "SessionStart"} doesn't contain key 'label' or 'outcome' .

One solution might be using all() to keep only the dictionaries which contains all keys in the key_list :

logs = [
    {"pytest_version": "7.1.e", "$report_type": "SessionStart"},
    {"label": "test1", "outcome": "passed", "duration": 0.0009},
    {"label": "test2", "outcome": "passed", "duration": 0.00019},
]

key_list = ["label", "outcome"]

print(
    [{k: d[k] for k in key_list} for d in logs if all(k in d for k in key_list)]
)

Prints:

[{'label': 'test1', 'outcome': 'passed'}, {'label': 'test2', 'outcome': 'passed'}]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM