You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用itertools实现滑动窗口处理数据集并输出指定Markdown表格求助

修改后的完整代码

运行后可直接输出符合要求的Markdown表格,表格行数会随数据集、滑动窗口参数自动调整:

import itertools as it
import numpy as np
import pandas as pd

x = [1,2,3,4,5,6,7,8,9,10]
def moving_window(x, length, step=1):
    streams = it.tee(x, length)
    return zip(*[it.islice(stream, i, None, step+2) for stream, i in zip(streams,it.count(step=step))])
x_ = list(moving_window(x,6))

def mean(dataset):
    return sum(dataset) / len(dataset)

# 输出指定格式Markdown表格
print("| A header | Another header |")
print()
print()
for idx, window in enumerate(x_, start=1):
    window_avg = mean(window)
    print(f"| a{idx}   | {window_avg}           |")
    print()

运行输出效果

| A header | Another header |


| a1   | 3.5           |

| a2   | 6.5           |

内容的提问来源于stack exchange,提问作者sanaz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 03:45:01