数据来源:Wind
1.创建上表中指数日涨跌幅的列表,并且要求每个指数对应一个列表
dj_list = [0.012717,-0.003021,0.001490,0.006351,-0.001653,-0.007230,0.000252,0.010299,-0.001042,0.002573,-0.000118,0.004159] #道琼斯工业平均指数 hs_list = [0.017578,0.002119,0.012210,-0.001670,0.004704,0.002671,-0.-0.001258,-0.009300,0.002356,-0.003311,0.010706,-0.000172,-0.005358]#恒生指数 sz_list = [0.025755,0.002038,0.012425,0.009413,-0.000543,-0.001586,0.000699,-0.016030,-0.000419,-0.003399,0.023857,0.002926,-0.003958]#上证综指 sc_list =[0.036423,-0.000715,0.007812,0.007281,-0.006138,0.008187,-0.000147,-0.026514,-0.002566,-0.007755,0.023263,0.005520,-0.005487]#深证成指
2.访问包含道琼斯工业平均指数日涨跌幅列表,找出首次跌幅超过-0.6%这一日之前的全部交易日涨跌幅数据
for i in dj_list: if i < -0.0006: break print('已经访问的道琼斯工业平均指数涨跌幅数据',i)
3.访问包含恒生指数日涨跌幅列表,找出交易日当天指数下跌的全部数据
第一种解法:
for i in hs_list: if i > 0: pass #跳过不执行该语句 else: print('恒生指数日跌幅数据',i)
第二种方法:
for i in hs_list: if i > 0: continue #表示终止并出该次循环,直接执行下一次循环 print('恒生指数日跌幅数据',i)
4.访问包含上证综指的日涨跌幅列表,找出日涨跌幅处于【-1%,1%】区间的数据并创建一个新的列表;同时,访问包含深证成指的日涨跌幅列表,找出日涨跌幅超过0.5%的数据并创建一个新的列表
1)
#创建一个新的上证综指列表,元素在[-1%,1%]区间 new_sz_list = [] for i in sz_list: if i < -0.01: pass elif i > 0.01: pass else: new_sz_list.append(i) print('上证综合指日涨跌幅处于-1%至1%区间的列表n',new_sz_list)
2)
#创建一个新的深证成指列表,元素在大于0.5% new_sc_list = [] for i in sc_list: if i <0.005: pass else: new_sc_list.append(i) print('深证成指日涨跌幅超过0.5%的列表n',new_sc_list)
题目来源:斯文《Python金融实战案例精粹》
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)