查找平均蟒蛇的技术

时间:2020-05-28 05:41:43

标签: python-3.x

我正在执行代码审查,需要帮助。这是我的数据集。

[{'Date': '22-Aug-2019', 'Open': 10905.3, 'High': 10908.25, 'Low': 10718.3, 'Close': 10741.35, 'Shares Traded': 668193449, 'Turnover (Rs. Cr)': 18764.38},
{'Date': '23-Aug-2019', 'Open': 10699.6, 'High': 10862.55, 'Low': 10637.15, 'Close': 10829.35, 'Shares Traded': 667079625, 'Turnover (Rs. Cr)': 20983.75}, {'Date': '26-Aug-2019', 'Open': 11000.3, 'High': 11070.3, 'Low': 10756.55, 'Close': 11057.85, 'Shares Traded': 684141923, 'Turnover (Rs. Cr)': 22375.99}]

我在使用该数据集来计算每月的未平仓量和关闭率之和。

       month_wise_open = {}
        month_wise_close = {}
        for share in dataset:
            month_name = datetime.datetime.strptime(share['Date'], "%d-%b-%Y").strftime('%B')
            if month_name not in month_wise_open.keys():
                month_wise_open[month_name] = share['Open']
                month_wise_close[month_name] = share['Close']
            else:
                month_wise_open[month_name] += share['Open']
                month_wise_close[month_name] += share['Close']
        month_wise = {'open': month_wise_open, 'close': month_wise_close}

1 个答案:

答案 0 :(得分:0)

    month_wise_open,month_wise_close,month_wise_open_result,month_wise_close_result = {},{},{},{}
    for share in dataset:
        month_name = datetime.datetime.strptime(share['Date'], "%d-%b-%Y").strftime('%B')
        if month_name not in month_wise_open.keys():
            month_wise_open[month_name] = [share['Open']]
            month_wise_close[month_name] = [share['Close']]
            month_wise_open_result[month_name] = share['Open']
            month_wise_close_result[month_name] = share['Close']
        else:
            month_wise_open[month_name] += [share['Open']]
            month_wise_close[month_name] += [share['Close']]
            month_wise_open_result[month_name] = sum(month_wise_open[month_name])/len(month_wise_open[month_name])
            month_wise_close_result[month_name] = sum(month_wise_close[month_name])/len(month_wise_close[month_name])
    month_wise = {'open': month_wise_open_result, 'close': month_wise_close_result}