Python 遍历列表添加到字典
Python loop through list add to dictionary
['TSLA220610C00200000', '2022-06-02 1:41PM EDT', '200.00', '580.56', '502.45', '504.35', '0.00', '-', '1', '3', '431.84%', 'TSLA220610C00350000', '2022-06-03 2:29PM EDT', '350.00', '358.80', '352.60', '354.50', '-65.17', '-15.37%', '3', '3', '50.00%']
calls = {"Contract Name":"['TSLA220610C00200000','TSLA220610C00350000']",
"Last Trade Date":"['2022-06-02 1:41PM EDT','2022-06-03 2:29PM EDT']",
"Strike":"['200.00','300.00']",
"Last Price":"['580.56','358.80']",
"Bid":"['502.45','352.60']",
"Ask":"['504.35','354.50']",
"Change":"['0.00','-65.17']",
"% Change":"['-','-15.37%']",
"Volume":"['1','3']",
"Open Interest":"['3','3']",
"Implied Volatility":"['431.84%','50.00%']"}
我正在尝试遍历此列表并将其添加到上面的字典中,这可能吗?
如果 lst
是您从问题中得到的列表,您可以尝试:
calls = {
"Contract Name": [],
"Last Trade Date": [],
"Strike": [],
"Last Price": [],
"Bid": [],
"Ask": [],
"Change": [],
"% Change": [],
"Volume": [],
"Open Interest": [],
"Implied Volatility": [],
}
for i in range(0, len(lst), len(calls)):
for v, l in zip(lst[i : i + len(calls)], calls.values()):
l.append(v)
print(calls)
打印:
{
"Contract Name": ["TSLA220610C00200000", "TSLA220610C00350000"],
"Last Trade Date": ["2022-06-02 1:41PM EDT", "2022-06-03 2:29PM EDT"],
"Strike": ["200.00", "350.00"],
"Last Price": ["580.56", "358.80"],
"Bid": ["502.45", "352.60"],
"Ask": ["504.35", "354.50"],
"Change": ["0.00", "-65.17"],
"% Change": ["-", "-15.37%"],
"Volume": ["1", "3"],
"Open Interest": ["3", "3"],
"Implied Volatility": ["431.84%", "50.00%"],
}
['TSLA220610C00200000', '2022-06-02 1:41PM EDT', '200.00', '580.56', '502.45', '504.35', '0.00', '-', '1', '3', '431.84%', 'TSLA220610C00350000', '2022-06-03 2:29PM EDT', '350.00', '358.80', '352.60', '354.50', '-65.17', '-15.37%', '3', '3', '50.00%']
calls = {"Contract Name":"['TSLA220610C00200000','TSLA220610C00350000']",
"Last Trade Date":"['2022-06-02 1:41PM EDT','2022-06-03 2:29PM EDT']",
"Strike":"['200.00','300.00']",
"Last Price":"['580.56','358.80']",
"Bid":"['502.45','352.60']",
"Ask":"['504.35','354.50']",
"Change":"['0.00','-65.17']",
"% Change":"['-','-15.37%']",
"Volume":"['1','3']",
"Open Interest":"['3','3']",
"Implied Volatility":"['431.84%','50.00%']"}
我正在尝试遍历此列表并将其添加到上面的字典中,这可能吗?
如果 lst
是您从问题中得到的列表,您可以尝试:
calls = {
"Contract Name": [],
"Last Trade Date": [],
"Strike": [],
"Last Price": [],
"Bid": [],
"Ask": [],
"Change": [],
"% Change": [],
"Volume": [],
"Open Interest": [],
"Implied Volatility": [],
}
for i in range(0, len(lst), len(calls)):
for v, l in zip(lst[i : i + len(calls)], calls.values()):
l.append(v)
print(calls)
打印:
{
"Contract Name": ["TSLA220610C00200000", "TSLA220610C00350000"],
"Last Trade Date": ["2022-06-02 1:41PM EDT", "2022-06-03 2:29PM EDT"],
"Strike": ["200.00", "350.00"],
"Last Price": ["580.56", "358.80"],
"Bid": ["502.45", "352.60"],
"Ask": ["504.35", "354.50"],
"Change": ["0.00", "-65.17"],
"% Change": ["-", "-15.37%"],
"Volume": ["1", "3"],
"Open Interest": ["3", "3"],
"Implied Volatility": ["431.84%", "50.00%"],
}