0
Follow
0
View

Question about #python# : Add up each term

edream_zhao 注册会员
2023-02-28 18:25

import numpy as np
import pandas as pd

data = [12, 13, 15, 16, 14, 11, 19, 24, 28, 25]
arr = np.array(data)
n = len(arr)

sum_3 = np.full(n, np.nan)
sum_5 = np.full(n, np.nan)

sum_3[2:] = arr[:-2] + arr[1:-1] + arr[2:] # 每三项求和
sum_5[4:] = arr[:-4] + arr[1:-3] + arr[2:-2] + arr[3:-1] + arr[4:] # 每五项求和

df = pd.DataFrame({'num': arr, 'sum_3': sum_3, 'sum_5': sum_5})
print(df)

dyb6918 注册会员
2023-02-28 18:25

you can use first n and Sn, a + a [2] + [1] [3] a [3] = S - S [0].
Sn is calculated using S[n]=S[n-1]+a[n], one cycle

About the Author

Question Info

Publish Time
2023-02-28 18:25
Update Time
2023-02-28 18:25

Related Question

C语言与Python的函数代码有啥不一样?

如何通过python weka wrapper进行深度学习?

请问opencv- python如何在一个窗口中显示两个图片,并且每个图片都有标题呢?

python写柱状图

python模拟S-MAC

Python代码编写

Python中导入模块中的函数运行出错

Python获取302重定向后的Location网址url

Python PyQT5 WebEngine分段错误

Python求1!-2!+3!-4!……-20!的和