weixin_43897229
墨玉世家
采纳率50%
2019-08-12 09:41 阅读 779

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条回答 默认 最新

  • zhanghuan_00001 zhanghuan_00001 2019-08-13 10:53

    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)
    代码的简写形式

    点赞 1 评论 复制链接分享

相关推荐