Python制作交易收盘价走势图中天均值,语句wd.index(w) + 1 for w in weekdays[1:idx_week]是什么意思

import json

filename = 'btc_close_2017.json'

with open(filename) as f:
btc_data = json.load(f)

dates, months, weeks, weekdays, closes = [], [], [], [], []

for btc_dict in btc_data:
dates.append(btc_dict['date'])
months.append(int(btc_dict['month']))
weeks.append(int(btc_dict['week']))
weekdays.append(btc_dict['weekday'])
closes.append(int(float(btc_dict['close'])))

import pygal
import math
from itertools import groupby

def draw_line(x_data, y_data, title, y_legend):
xy_map = []
for x, y in groupby(sorted(zip(x_data, y_data)), key=lambda _ : _[0]):
y_list = [v for _, v in y]
xy_map.append([x, sum(y_list) / len(y_list)])
x_unique, y_mean = [*zip(*xy_map)]
line_chart = pygal.Line()
line_chart.title = title
line_chart.x_labels = x_unique
line_chart.add(y_legend, y_mean)
line_chart.render_to_file(title+'.svg')
return line_chart

def draw_line_weekday(x_data, y_data, title, y_legend):
xy_map = []
for x, y in groupby(sorted(zip(x_data, y_data)), key=lambda _ : _[0]):
y_list = [v for _, v in y]
xy_map.append([x, sum(y_list) / len(y_list)])
x_unique, y_mean = [*zip(*xy_map)]
line_chart = pygal.Line()
line_chart.title = title
#line_chart.x_labels = x_unique
line_chart.x_labels = ['周一','周二','周三','周四','周五','周六','周日']
line_chart.add(y_legend, y_mean)
line_chart.render_to_file(title+'.svg')
return line_chart

idx_week = dates.index('2017-12-11')
wd = ['Monday', 'Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday']
weekdays_int = [wd.index(w) + 1 for w in weekdays[1:idx_week]]
line_chart_weekday = draw_line_weekday(weekdays_int, closes[1:idx_week], '收盘价周均值(¥)', '周均值')

line_chart_weekday

1个回答

wd = [ "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
ss = [ "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" ]

weekdays_int = [ wd.index(w) + 1 for w in ss[0:]]
print( "LOG:", weekdays_int)

weekdays_compare = []
for w in ss[0:]:
weekdays_compare.append( wd.index(w) + 1)

print( "LOG:", weekdays_compare)
通过上述代码实验可知:[ wd.index(w) + 1 for w in ss[0:]]是
for w in ss[0:]:
weekdays_compare.append( wd.index(w) + 1)
代码的简写形式

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问