简体   繁体   中英

Convert dictionary lists to multi-dimensional list of dictionaries

I've been trying to convert the following:

data = {'title':['doc1','doc2','doc3'], 'name':['test','check'], 'id':['ddi5i'] }

to:

[{'title':'doc1', 'name': 'test', 'id': 'ddi5i'},
{'title':'doc2', 'name': 'test', 'id': 'ddi5i'},
{'title':'doc3', 'name': 'test', 'id': 'ddi5i'},
{'title':'doc1', 'name': 'check', 'id': 'ddi5i'},
{'title':'doc2', 'name': 'check', 'id': 'ddi5i'},
{'title':'doc3', 'name': 'check', 'id': 'ddi5i'}]

I've tried various options (list comprehensions, pandas and custom code) but nothing seems to work. For example, the following:

panda.DataFrame(data).to_dict('list')

throws an error because, since it tries to map the lists, all of them have to be of the same length. Besides, the output would only be uni-dimensional which is not what I'm looking for.

itertools.product may be what you're looking for here, and it can be applied to the values of your data to get appropriate value groupings for the new dicts. Something like

list(dict(zip(data, ele)) for ele in product(*data.values()))

Demo

>>> from itertools import product
>>> list(dict(zip(data, ele)) for ele in product(*data.values()))
[{'id': 'ddi5i', 'name': 'test', 'title': 'doc1'},
 {'id': 'ddi5i', 'name': 'test', 'title': 'doc2'},
 {'id': 'ddi5i', 'name': 'test', 'title': 'doc3'},
 {'id': 'ddi5i', 'name': 'check', 'title': 'doc1'},
 {'id': 'ddi5i', 'name': 'check', 'title': 'doc2'},
 {'id': 'ddi5i', 'name': 'check', 'title': 'doc3'}]

It is clear how this works once seeing

>>> list(product(*data.values()))
[('test', 'doc1', 'ddi5i'),
 ('test', 'doc2', 'ddi5i'),
 ('test', 'doc3', 'ddi5i'),
 ('check', 'doc1', 'ddi5i'),
 ('check', 'doc2', 'ddi5i'),
 ('check', 'doc3', 'ddi5i')]

and now it is just a matter of zipping back into a dict with the original keys.

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