I am trying to create dict
by nested list
:
groups = [[\'Group1\', \'A\', \'B\'], [\'Group2\', \'C\', \'D\']]
L = [{y:x[0] for y in x i
-
I also like Willem's solution, but just for completeness...
another variation using itertools and a generator function (Python 3.x only)
def pairs(groups):
for value,*keys in groups:
for key_value in zip(keys, itertools.repeat(value)):
yield key_value
dict(pairs(groups))
{'A': 'Group1', 'B': 'Group1', 'C': 'Group2', 'D': 'Group2'}
- 热议问题