How to Create Nested Dictionary in Python with 3 lists

后端 未结 7 2045
广开言路
广开言路 2020-12-03 19:31

Let us Suppose, I have created 3 lists and I want to create a dictionary for it. e.g.

a= [\'A\', \'B\', \'C\', \'D\']
b =[1, 2, 3, 4]
c = [9, 8, 7, 6]
         


        
7条回答
  •  囚心锁ツ
    2020-12-03 19:52

    You can also use map() here:

    a = ['A', 'B', 'C', 'D']
    b = [1, 2, 3, 4]
    c = [9, 8, 7, 6]
    
    dct = dict(map(lambda x, y, z : (x, {str(y): str(z)}), a, b, c))
    
    print(dct)
    

    Which outputs:

    {'A': {'1': '9'}, 'B': {'2': '8'}, 'C': {'3': '7'}, 'D': {'4': '6'}}
    

提交回复
热议问题