简体   繁体   中英

Is there an elegant way to create a dictionary of arrays?

I frequently analyse data which has a parent child relationship:

data = [
    {'data': 'somedata', 'id': 1, 'parentId': 0},
    {'data': 'somedata', 'id': 2, 'parentId': 1},
    {'data': 'somedata', 'id': 3, 'parentId': 0},
    {'data': 'somedata', 'id': 4, 'parentId': 3},
    {'data': 'somedata', 'id': 5, 'parentId': 3},
]

Typically I'll use a loop like this to create a new data structure so I can easily relate parent and child data:

for item in data:
    if item["parentId"] != 0:
        if item["parentId"] in parents:
            parents[item["parentId"]].append(item["id"])
        else:
            parents[item["parentId"]] = []
            parents[item["parentId"]].append(item["id"])

This produces the following data:

print parents
{1: [2], 3: [4, 5]}

Is there a more elegant way to create the "parents" data structure?

I don't know, what do you mean by more elegant. If you are writing some parsing script, than it seems that using built in structures is ok. So are you asking about used data structures or your code?

One thing I see is that you can use setdefault instead of checking if you have certain key in dictionary:

for item in data:
    if item["parentId"] != 0:
        parents.setdefault(item["parentId"], []).append(item['id'])

If your Python version includes collections.defaultdict you can do:

from collections import defaultdict

def make_parent_dict(data):
    parents = defaultdict(list) # Use empty list for missing entries
    for item in data:
        parent = item['parentId']
        if parent != 0:
            parents[parent].append(item['id'])
    return dict(parents) # Convert back to normal dict

example = [
    {'data': 'somedata', 'id': 1, 'parentId': 0},
    {'data': 'somedata', 'id': 2, 'parentId': 1},
    {'data': 'somedata', 'id': 3, 'parentId': 0},
    {'data': 'somedata', 'id': 4, 'parentId': 3},
    {'data': 'somedata', 'id': 5, 'parentId': 3},
]

>>> print make_parent_dict(example)
{1: [2], 3: [4, 5]}

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