python - 如何将列表列表中的值分配给键以在python中创建字典
问题描述
我有一个键列表和值列表。
['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']
]
我有兴趣从两个中创建一个字典。请注意顺序也很重要。这是我的预期输出。
{
{'OpenInterest_x': '150', ...},
{'OpenInterest_x': '-', ...},
}
我不能在这里使用 zip 因为它会将完整的第一个列表分配给第一个键。做这个的最好方式是什么?
解决方案
如果您的第一个列表被称为headers
,而您的第二个列表仍然是一个列表列表values
,则可以执行以下操作:
list_of_dicts = [dict(zip(headers, sublist)) for sublist in values]
推荐阅读
- python-3.x - 从sqlalchemy中的相同多态类型继承的两个模型的多对一关系
- exception - kotlin 如何将受检异常更改为运行时异常?
- reactjs - React Router,删除某些路线中的导航/侧边栏
- unit-testing - 当测试文件被定义为模块时,使用堆栈调用的 hspec 定义的测试会引发错误
- excel - 当单元格仍在调整大小范围内时,保留数据的文本更改
- javascript - 当子道具动态变化时,对象作为 React 子对象无效
- javascript - 如何在node.js中将json数组数据插入mysql?
- c - 在结构内的指针上使用 free() 可以为整个结构释放内存
- python - 如何打开 Firefox 并将新配置文件设置为默认配置?(硒和 Python)
- python-3.x - 在 ubuntu 18.04 上恢复 python3.6