Nested list to dict

后端 未结 4 1737
忘掉有多难
忘掉有多难 2021-02-18 14:02

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         


        
4条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-18 14:33

    I think one line solution is a bit confusion. I would write code like below

    groups = [['Group1', 'A', 'B'], ['Group2', 'C', 'D']]
    
    result = {}
    for group in groups:
        for item in group[1:]:
            result[item] = group[0]
    print result
    

提交回复
热议问题