Using default dictionaries problem (python)

Question:

I have a slightly weird input of data that is in this format:

data = { 'sensor1': {'units': 'x', 'values': [{'time': 17:00, 'value': 10},
                                       {'time': 17:10, 'value': 12}, 
                                       {'time': 17:20, 'value' :7}, ...]}
  'sensor2': {'units': 'x', 'values': [{'time': 17:00, 'value': 9},
                                       {'time': 17:20, 'value': 11}, ...]}
}

And I want to collect the output to look like:

{'17:00': [10,9], '17:10': [12,], '17:20': [7,11], ... }

So the keys are the unique timestamps (ordered) and the values are a list of the values of each sensor, in order they come in the original dictionary. If there is no value for the timestamp in one sensor, it is just left as an empty element ”. I know I might need to use defaultdict but I’ve not had any success.

e.g.

    s = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)]
    d = defaultdict(list)
        for k, v in s:
            d[k].append(v)
   
    sorted(d.items())
    [('blue', [2, 4]), ('red', [1]), ('yellow', [1, 3])]
    d = defaultdict(default_factory=list)
    values_list = data.values()
    for item in values_list:
        for k, v in item['values']:
            d[k].append(v)

    result = sorted(d.items())

Encounters key error as each item in values_list is not a tuple but a dict.

Asked By: Josh Brumpton

||

Answers:

You can also use dict in this way:

data = {'sensor1': {'units': 'x', 'values': [{'time': '17:00', 'value': 10},
                                             {'time': '17:10', 'value': 12},
                                             {'time': '17:20', 'value': 7},
                                             ]},
        'sensor2': {'units': 'x', 'values': [{'time': '17:00', 'value': 9},
                                             {'time': '17:20', 'value': 11},
                                             ]}
        }

d = {}
for item in data.values():
    for pair in item['values']:
        if pair["time"] in d:
            d[pair["time"]].append(pair["value"])
        else:
            d[pair["time"]] = [pair["value"]]

result = sorted(d.items())
print(result)

Output:

[('17:00', [10, 9]), ('17:10', [12]), ('17:20', [7, 11])]

Using defaultdict defaultdict example with list in Python documentation :

from collections import defaultdict

data = {'sensor1': {'units': 'x', 'values': [{'time': '17:00', 'value': 10},
                                             {'time': '17:10', 'value': 12},
                                             {'time': '17:20', 'value': 7},
                                             ]},
        'sensor2': {'units': 'x', 'values': [{'time': '17:00', 'value': 9},
                                             {'time': '17:20', 'value': 11},
                                             ]}
        }

d = defaultdict(list)
for item in data.values():
    for pair in item['values']:
        d[pair["time"]].append(pair["value"])
result = sorted(d.items())
print(result)

Output:

[('17:00', [10, 9]), ('17:10', [12]), ('17:20', [7, 11])]
Answered By: arsho