简体   繁体   中英

Create dictionary from multi-dimensional list without duplicating keys

I'd like to take a list of tuples with multiple elements and turn it into a multi-dimensional dictionary without repeating keys. So if the following is my original list:

myList = [('jeep', 'red', 2002, 4), ('jeep', 'red', 2003, 6), ('jeep', 'blue', 2003, 4), ('bmw', 'black', 2015, 8)]

I would want to take the above and turn it into a dictionary in this format:

{'jeep':
    {'red': [
        [2002, 4],
        [2003, 6]]
    'blue': [
        [2003, 4]]
    },
'bmw':
    {'black': [
        [2015, 8]]
    }
}

I seemed to be on the right path with Python's defaultdict, but I can't seem to completely solve this. Thanks!

Using a lot of dict.setdefault ...

myList = [('jeep', 'red', 2002, 4), ('jeep', 'red', 2003, 6), ('jeep', 'blue', 2003, 4), ('bmw', 'black', 2015, 8)]

d = {}
for model, colour, year, month in myList:
    d.setdefault(model, {}).setdefault(colour, []).append([year, month])

For each item in myList , either get the current dictionary for the model or create the key with a new empty dict, then with that dict, either retrieve the list for that colour, or set the key with a new empty list, then append the year and month as a 2-element list to that list...

Gives you d :

{'bmw': {'black': [[2015, 8]]},
 'jeep': {'blue': [[2003, 4]], 'red': [[2002, 4], [2003, 6]]}}

Since what you want is essentially a tree data structure with a certain number of levels with lists for leaves, I'd be more explicit about it all by encapsulating the details inside a custom dictionary subclass because it could make the conversion extremely simple.

Here's a generic version of the data structure applied to your data:

class TreeContainer(dict):
    def __init__(self, max_levels, leaf_factory=lambda: None, level=1):
        self.max_levels = max_levels
        self.level = level
        self.leaf_factory = leaf_factory

    def __missing__(self, key):
        if self.level < self.max_levels:  # need another level?
            value = self[key] = type(self)(self.max_levels, self.leaf_factory, 
                                           self.level+1)
        else:
            value = self[key] = self.leaf_factory()
        return value

myList = [("jeep", "red", 2002, 4), ("jeep", "red", 2003, 6),
          ("jeep", "blue", 2003, 4), ("bmw", "black", 2015, 8)]

vehicles = TreeContainer(2, list)

for model, color, year, month in myList:  # convert list to dictionary
    vehicles[model][color].append([year, month])

from pprint import pprint
pprint(vehicles)

Output:

{'bmw': {'black': [[2015, 8]]},
 'jeep': {'blue': [[2003, 4]], 'red': [[2002, 4], [2003, 6]]}}

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