'python how to create dictionaries in dictionaries from lists
I have a list of file names, experiments = ['f1','f2','f3','f4'], times of day, t = ['am','pm'], and types of data collected, ['temp','humidity'].
From these I want to create dictionaries within dictionaries in the following format:
dict = {'f1': { am : {'temp': [], 'humidity': []} , pm : {'temp': [], 'humidity': []}},
'f2': { am : {'temp': [], 'humidity': []} , pm : {'temp': [], 'humidity': []}},
'f3': { am : {'temp': [], 'humidity': []} , pm : {'temp': [], 'humidity': []}},
'f4': { am : {'temp': [], 'humidity': []} , pm : {'temp': [], 'humidity': []}}}
What's the best way to do this?
Solution 1:[1]
A case for comprehensions if I ever saw.
from copy import deepcopy
datatypes = ['temp','humidity']
times = ['am','pm']
experiments = ['f1','f2','f3','f4']
datatypes_dict = dict((k, []) for k in datatypes)
times_dict = dict((k, deepcopy(datatypes_dict)) for k in times)
experiments_dict = dict((k, deepcopy(times_dict)) for k in experiments)
or the nicer dict comprehension way (python 2.7+)
datatypes_dict = {k: [] for k in datatypes}
times_dict = {k: deepcopy(datatypes_dict) for k in times}
experiments_dict = {k: deepcopy(times_dict) for k in experiments}
you can nest them but it gets mind-blowing pretty quick if things are at all complicated.
In this use case, however, @marshall.ward's answer
{z: {y: {x: [] for x in data_types} for y in t} for z in experiments}
is far better than mine, as you can avoid the deepcopy()ing.
Solution 2:[2]
Taking some artistic license with the output format
>>> from collections import namedtuple, defaultdict
>>> from itertools import product
>>> experiments = ['f1','f2','f3','f4']
>>> times_of_day = ['am','pm']
>>> data_types = ['temp','humidity']
>>> DataItem = namedtuple('DataItem', data_types)
>>> D=defaultdict(dict)
>>> for ex, tod in product(experiments, times_of_day):
... D[ex][tod]=DataItem([], [])
...
>>> D
defaultdict(<type 'dict'>, {'f1': {'am': DataItem(temp=[], humidity=[]), 'pm': DataItem(temp=[], humidity=[])}, 'f2': {'am': DataItem(temp=[], humidity=[]), 'pm': DataItem(temp=[], humidity=[])}, 'f3': {'am': DataItem(temp=[], humidity=[]), 'pm': DataItem(temp=[], humidity=[])}, 'f4': {'am': DataItem(temp=[], humidity=[]), 'pm': DataItem(temp=[], humidity=[])}})
You can access the data items like this
>>> D['f1']['am'].temp
[]
>>> D['f1']['am'].humidity
[]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | John La Rooy |
