Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Using default dictionaries problem (python)

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

    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.

>Solution :

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 = {}
values_list = data.values()
for item in values_list:
    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)
values_list = data.values()
for item in values_list:
    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])]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading