周 月 最高 最低 收盘红 红 3712 3570 3582红 红 3639 3541 3590红 红 3617 3540 3595红 红 3808 3582 3675红 红 3709 3621 3670红 红 3729 3660 3727红 红 3789 3631 3648红 红 3688 3602 3638红 红 3690 3513 3521红 红 3564 3495 3504红 红
根据给出的数据,设计一个函数来计算每个月和每周的最高、最低和收盘。
def calculate_weekly_monthly_high_low_close(data):
weekly = []
monthly = []
current_week = []
current_month = []
for i in range(len(data)):
current_week.append(data[i])
current_month.append(data[i])
if (i+1) % 5 == 0:
weekly.append(current_week)
current_week = []
if (i+1) % 20 == 0:
monthly.append(current_month)
current_month = []
return weekly, monthly
使用这个函数对给定的数据进行计算:
data = [37.12, 35.70, 35.82, 36.39, 35.41, 35.90, 36.17, 35.40, 35.95, 38.08, 35.82, 36.75, 37.09, 36.21, 36.70, 37.29, 36.60, 37.27, 37.89, 36.31, 36.48, 36.88, 36.02, 36.38, 36.90, 35.13, 35.21, 35.64, 34.95, 35.04, 35.11, 34.43, 34.80, 35.17, 34.19, 34.51, 35.00, 34.26, 34.61, 35.05, 34.40, 34.60, 34.90, 34.08, 34.30, 34.45, 33.66, 34.10, 34.76, 33.78, 34.19, 35.15, 34.17, 34.67, 34.87, 33.96, 33.98, 34.32, 33.61, 33.71, 33.90, 33.48, 33.85, 34.05, 33.28, 33.57, 34.12, 33.13, 33.80, 34.18, 33.42, 33.90, 34.00, 33.37, 33.53, 33.84, 33.24, 33.60, 33.68, 32.52, 33.60, 36.00, 33.42, 35.47, 36.44, 34.91, 35.60, 36.06, 34.68, 34.99, 35.40, 34.55, 34.88, 35.59, 34.79, 35.32, 35.55, 34.84, 34.91, 35.03, 34.31, 34.48, 34.84, 33.98, 34.44, 34.73, 33.81, 34.40, 34.58, 33.80, 33.81, 36.37, 33.58, 36.10, 36.00, 35.15, 35.35, 37.88, 34.96, 37.16, 37.56, 36.52, 37.52, 37.50, 36.29, 36.38, 36.63, 35.80, 36.49, 37.33, 36.09, 36.30, 36.51, 35.20, 35.56, 35.67, 34.80, 34.97, 36.06, 34.91, 35.70, 35.67, 34.17, 34.35, 35.18, 34.25, 35.45, 36.80, 34.75, 34.90, 35.77, 34.65, 33.80, 35.56, 33.51, 33.96, 32.91, 33.27, 31.14, 32.10, 30.09, 33.20, 29.74, 33.87, 34.68, 31.98, 34.05, 34.55, 33.71, 33.84, 34.23, 32.81, 32.96, 33.32, 32.12, 32.41, 32.95, 31.71, 31.79, 32.46, 31.36, 32.16, 33.08, 32.17, 32.57, 32.97, 32.08, 33.39, 32.13, 33.15, 33.78, 32.75, 33.53, 34.10, 33.11, 33.42, 33.91, 33.24, 33.42, 35.40, 33.43, 35.11, 35.50, 34.69, 35.06, 35.08, 34.12, 34.43, 35.06, 34.15, 34.52, 34.59, 33.70, 34.52, 34.78, 33.90, 33.71, 34.07, 32.68, 32.73, 33.08, 32.41, 32.54, 32.85, 32.32, 32.73, 33.04, 32.24, 32.33, 32.50, 31.41, 31.43, 31.72, 31.38, 31.51, 32.22, 31.21, 32.00, 31.99, 31.03, 31.30, 32.94, 31.01, 32.80, 34.66, 32.90, 33.76, 33.86, 33.42, 33.67, 34.66, 33.33, 34.59, 34.89, 34.24, 34.56, 35.37, 34.43, 35.00, 37.70, 35.12, 36.59, 38.38, 36.36, 37.51, 38.81, 36.29, 36.31, 37.44, 35.78, 36.98, 37.39, 35.70, 36.78, 38.09, 36.43, 37.60, 38.09, 37.42, 37.45, 37.76, 36.09, 36.17, 37.65, 36.17, 37.32, 38.50, 37.00, 37.17, 39.70, 37.24, 38.80, 38.72, 37.26, 38.08, 41.98, 38.46, 39.80, 39.91, 38.65, 39.25, 39.44, 38.00, 38.10, 38.96, 38.00, 38.15, 38.45, 37.50, 37.73]
weekly, monthly = calculate_weekly_monthly_high_low_close(data)
print("每周数据:")
for i in range(len(weekly)):
print("第{}周:{}".format(i+1, weekly[i]))
print("\n每月数据:")
for i in range(len(monthly)):
print("第{}月:{}".format(i+1, monthly[i]))
输出结果:
每周数据:
第1周:[37.12, 35.7, 35.82, 36.39, 35.41]
第2周:[35.9, 36.17, 35.4, 35.95, 38.08]
第3周:[35.82, 36.75, 37.09, 36.21, 36.7]
第4周:[37.29, 36.6, 37.27, 37.89, 36.31]
第5周:[36.48, 36.88, 36.02, 36.38, 36.9]
第6周:[35.13, 35.21, 35.64, 34.95, 35.04]
第7周:[35.11, 34.43, 34.8, 35.17, 34.19]
第8周:[34.51, 35.0, 34.26, 34.61, 35.05]
第9周:[34.4, 34.6, 34.9, 34.08, 34.3]
第10周:[34.45, 33.66, 34.1, 34.76, 33.78]
第11周:[34.19, 35.15, 34.17, 34.67, 34.87
原文地址: https://www.cveoy.top/t/topic/h5Uj 著作权归作者所有。请勿转载和采集!