如何将列表列表中的值分配给键以在python中创建字典

Dev*_*wal 1 python dictionary list

我有一个键列表和值列表。

['OpenInterest_x', 'Change in Open Interest  x', 'LTP_x', 'NetChange_x', 'Volume_x', 'BidQty_x', 'BidPrice_x', 'OfferPrice_x', 'OfferQty_x', 'Strike Price', 'BidQty', 'BidPrice', 'OfferPrice', 'OfferQty', 'Volume', 'NetChange', 'LTP', 'OpenInterest', 'Change in Open Interest'
]

[
['150', '150', '1,070.00', '-928.90', '2', '450', '1,097.20', '1,314.15', '1,875', '4500.00', '375', '3.20', '10.25', '75', '-', '-', '-', '-', '-'], 
['-', '-', '-', '-', '-', '150', '1,001.90', '1,056.15', '150', '4600.00', '75', '7.00', '17.80', '150', '4', '-7.55', '10.00', '1,350', '300']
]
Run Code Online (Sandbox Code Playgroud)

我有兴趣从两个字典中创建一个。请注意订购也很重要。这是我的预期输出。

{
    {'OpenInterest_x': '150', ...},
    {'OpenInterest_x': '-', ...},

}
Run Code Online (Sandbox Code Playgroud)

我无法在此处使用zip,因为它将为第一个键分配完整的第一列表。做这个的最好方式是什么?

小智 7

如果第一个列表称为headers,而第二个列表仍称为values,则可以按以下步骤进行操作:

list_of_dicts = [dict(zip(headers, sublist)) for sublist in values]
Run Code Online (Sandbox Code Playgroud)