简体   繁体   中英

Grouping values to a list in dict

While reading a csv file using csv.DictReader

I get

[{'id': 1, 'status1': '1', 'status2': '2', 'status3': '3' }]

How can I manuplate while reading or later to get:

[{'id': 1, 'status': ['1', '2', '3']}]

TLDR; I want to group similar fields into a list.

and/or - how can i do this in pandas pd.read_csv() too?

Thanks in advance!

If it is certain that the only fields you want to group are those who end with digits, you can use a regex to identify them, and append their corresponding values to a list:

import re

def compact_dict(d):
    compact = {}
    for key, value in d.items():
        # a group containing anything, followed by at least one digit
        m = re.match(r'(.*)\d+', key)  
        if m:
            # the key that we use is the original one without the final digits
            compact.setdefault(m.group(1), []).append(value)
        else:
            # not part of a group of similar keys, we just store the key:value pair
            compact[key] = value
    return compact

data = [{'id': 1, 'status1': '1', 'status2': '2', 'status3': '3' }]
out = [compact_dict(d) for d in data]
print(out)
# [{'id': 1, 'status': ['1', '2', '3']}]

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