Split dictionary of lists into list of dictionaries

前端 未结 8 1846
时光取名叫无心
时光取名叫无心 2020-12-09 21:16

What i need to do is to convert something like this

{\'key1\': [1, 2, 3], \'key2\': [4, 5, 6]}

into

[{\'key1\': 1, \'key2\'         


        
8条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-09 21:32

    Without for loop, Internal process of map is iterating actually, just without the keyword for

    >>> x={'key1': [1, 2, 3], 'key2': [4, 5, 6]}
    
    >>> map(lambda x,y:{'key1':x,'key2':y},x['key1'],x['key2'])
    
    [{'key2': 4, 'key1': 1}, {'key2': 5, 'key1': 2}, {'key2': 6, 'key1': 3}]
    

提交回复
热议问题