Combined List and Understanding Dict

I have some verbose logic that I would like to combine with some understandings.

Essentially, I have a dict object that I am reading, which has 16 values ​​in which I am bound. I get the keys I want with the following understanding:

["I%d" % (i,) for i in range(16)] 

The type of source dictionary is as follows:

 { "I0": [0,1,5,2], "I1": [1,3,5,2], "I2": [5,9,10,1], ... } 

I would essentially display this dictionary as something like this:

 [ { "I0": 0, "I1": 1, "I2": 5, ... } { "I0": 1, "I1": 3, "I2": 9, ... } ... ] 

How can I match things with a list and a word understanding to convert the original dict to a mailing list in a mailing list?

+6
source share
3 answers

This is a fully functional solution that can be applied to any size.

 d = { "I0": [0,1,5,2], "I1": [1,3,5,2], "I2": [5,9,10,1]} map(dict, zip(*map(lambda (k, v): map(lambda vv: (k, vv), v), d.iteritems()))) 

to clarify: (I am using ipython , and the underscore _ means the previous output)

 In [1]: d = {'I0': [0, 1, 5, 2], 'I1': [1, 3, 5, 2], 'I2': [5, 9, 10, 1]} In [2]: map(lambda (k, v): map(lambda vv: (k, vv), v), _.iteritems()) Out[2]: [[('I1', 1), ('I1', 3), ('I1', 5), ('I1', 2)], [('I0', 0), ('I0', 1), ('I0', 5), ('I0', 2)], [('I2', 5), ('I2', 9), ('I2', 10), ('I2', 1)]] In [3]: zip(*_) Out[3]: [(('I1', 1), ('I0', 0), ('I2', 5)), (('I1', 3), ('I0', 1), ('I2', 9)), (('I1', 5), ('I0', 5), ('I2', 10)), (('I1', 2), ('I0', 2), ('I2', 1))] In [4]: map(dict, _) Out[4]: [{'I0': 0, 'I1': 1, 'I2': 5}, {'I0': 1, 'I1': 3, 'I2': 9}, {'I0': 5, 'I1': 5, 'I2': 10}, {'I0': 2, 'I1': 2, 'I2': 1}] 
+7
source

How would I decide:

First, I would get for each key a list containing tuples, where the first element would be the key, and the second would be one of the values ​​from the list:

 >>> [ [ (k, i) for i in l] for k, l in d.items() ] [[('I1', 1), ('I1', 3), ('I1', 5), ('I1', 2)], [('I0', 0), ('I0', 1), ('I0', 5), ('I0', 2)], [('I2', 5), ('I2', 9), ('I2', 10), ('I2', 1)]] 

Then I would cross out this list by creating a list of tuples containing each corresponding key using the zip function:

 >>> list(zip(*[ [ (k, i) for i in l] for k, l in d.items() ])) [(('I1', 1), ('I0', 0), ('I2', 5)), (('I1', 3), ('I0', 1), ('I2', 9)), (('I1', 5), ('I0', 5), ('I2', 10)), (('I1', 2), ('I0', 2), ('I2', 1))] 

These signatures can be passed as a parameter to the dict constructor:

 >>> [dict(lp) for lp in zip(*[ [ (k, i) for i in l] for k, l in d.items() ])] [{'I0': 0, 'I1': 1, 'I2': 5}, {'I0': 1, 'I1': 3, 'I2': 9}, {'I0': 5, 'I1': 5, 'I2': 10}, {'I0': 2, 'I1': 2, 'I2': 1}] 

In practice, however, I would never recommend doing such a thing on just one line:

 >>> pairs = [ [ (k, i) for i in l] for k, l in d.items() ] >>> transversed = zip(*pairs) >>> ds = [dict(t) for t in transversed] >>> pprint(ds) [{'I0': 0, 'I1': 1, 'I2': 5}, {'I0': 1, 'I1': 3, 'I2': 9}, {'I0': 5, 'I1': 5, 'I2': 10}, {'I0': 2, 'I1': 2, 'I2': 1}] 

Actually, I would say that I posted this answer mainly to suggest that you split your solution into more than one line.

+6
source

There is a short and simple solution:

 keys = data.keys() values = data.values() transformed = [dict(zip(keys, t)) for t in zip(*values)] 

The key point here is the transfer of the matrix of values, which is performed using zip(*values) , then we simply restore the dicts.

0
source

Source: https://habr.com/ru/post/906412/


All Articles