创建X个存储在for循环中的列表,然后保存在X个字典中



我正在尝试生成一些数据,并将其存储在字典中。目前我可以打印我想要的内容,但我似乎无法将其存储为列表,因为我无法为列表生成过程变量名称。

tickers = ['GOOG','GS','CL=F']
downloaded =
yf.download(tickers,period='1wk',interval='1d',auto_adjust=False,progess=True)
merged = downloaded.values.tolist()

for x1 in range(len(merged)):
print('Day:', x1 + 1)
for x in range(len(tickers)):
print(tickers[x],merged[x1][x::len(tickers)])
print("___________")

我正在尝试将打印的(股票行情(存储为列表。上述输出为:

Day: 3
CL=F [73.12999725341797, 73.12999725341797, 75.44000244140625, 72.20999908447266, 75.16999816894531, 528360.0]
GOOG [2641.64990234375, 2641.64990234375, 2659.919921875, 2637.9599609375, 2638.030029296875, 895600.0]
GS [374.3999938964844, 374.3999938964844, 381.7699890136719, 371.3900146484375, 378.75, 3295700.0]
___________
Day: 4
CL=F [71.6500015258789, 71.6500015258789, 72.95999908447266, 71.4000015258789, 72.95999908447266, 368679.0]
GOOG [2625.330078125, 2625.330078125, 2651.89990234375, 2611.9599609375, 2650.0, 829300.0]
GS [373.3500061035156, 373.3500061035156, 378.75, 370.57000732421875, 371.239990234375, 2183900.0]
etc...

因此,还有一个X天数或len(合并(,因此它将存储在字典中。

我试着把它放在嵌套的for循环中:('data'+tickers[x]) = merged[x1][x::len(tickers)]然而,python似乎并不喜欢这样。

所需输出为:

goog_data = {
day_1 : [list1]
day_2 : [list2]
day_3 : [list3]
} 
gs_data = {
day_1 : [list4]
day_2 : [list5]
day_3 : [list6]
}
etc...

tickers[x]+'_data' = {
day(len(merged)) : merged[x1][x::len(tickers)]
...
}

尝试下面的代码,它将生成您期望的输出

import yfinance as yf
tickers = ['GOOG','GS','CL=F']
downloaded = yf.download(tickers,period='1wk',interval='1d',auto_adjust=False,progess=True)
merged = downloaded.values.tolist()
goog_data = {}
gs_data = {}
clf_data = {}
for x1 in range(len(merged)):
dated = "day_"+str(x1+1)
for x in range(len(tickers)):
data_listed = merged[x1][x::len(tickers)]

if tickers[x]=="GOOG":
goog_data[dated] = data_listed
elif tickers[x]=="GS":
gs_data[dated] = data_listed
elif tickers[x]=="CL=F":
clf_data[dated] = data_listed

然后简单地打印字典

print("GOOG DATA:")
for key, val in goog_data.items():
print(key,val)

输出:

GOOG DATA:
day_1: [71.6500015258789, 71.6500015258789, 72.95999908447266, 71.4000015258789, 72.95999908447266, 368679.0]
day_2: [71.80999755859375, 71.80999755859375, 72.30000305175781, 70.41000366210938, 71.4800033569336, 133926.0]
day_3: [66.41999816894531, 66.41999816894531, 71.66999816894531, 65.62999725341797, 71.48999786376953, 82565.0]
day_4: [67.41999816894531, 67.41999816894531, 67.6500015258789, 65.20999908447266, 66.5999984741211, 82565.0]
day_5: [nan, nan, nan, nan, nan, nan]
day_6: [70.37000274658203, 70.37000274658203, 71.16000366210938, 69.86000061035156, 70.22000122070312, 174244.0]
GS DATA:
day_1: [2625.330078125, 2625.330078125, 2651.89990234375, 2611.9599609375, 2650.0, 829300.0]
day_2: [2636.909912109375, 2636.909912109375, 2643.659912109375, 2616.429931640625, 2632.820068359375, 742800.0]
day_3: [2585.080078125, 2585.080078125, 2624.93994140625, 2570.739990234375, 2623.110107421875, 1285500.0] 
day_4: [2622.030029296875, 2622.030029296875, 2640.027099609375, 2583.76806640625, 2600.080078125, 953300.0]
day_5: [2652.010009765625, 2652.010009765625, 2652.344970703125, 2612.030029296875, 2615.739990234375, 736200.0]
day_6: [2663.5400390625, 2663.5400390625, 2665.65185546875, 2648.0, 2653.0, 114773.0]
CL=F DATA:
day_1: [373.3500061035156, 373.3500061035156, 378.75, 370.57000732421875, 371.239990234375, 2183900.0]     
day_2: [364.79998779296875, 364.79998779296875, 374.92999267578125, 363.8299865722656, 374.92999267578125, 
2582200.0]
day_3: [354.7200012207031, 354.7200012207031, 355.19000244140625, 349.0, 354.0, 4394600.0]
day_4: [364.760009765625, 364.760009765625, 367.30999755859375, 351.5400085449219, 352.2699890136719, 2932200.0]
day_5: [373.5, 373.5, 374.8999938964844, 367.92999267578125, 368.0400085449219, 2396800.0]
day_6: [370.489990234375, 370.489990234375, 373.69378662109375, 369.2799987792969, 372.29998779296875, 215080.0]

相关内容

  • 没有找到相关文章

最新更新