我有 2 个 NumPy 数组,如下所示:
import numpy as np
a = np.array([1, 2, 2, 6, 4, 4, 6, 2, 7, 6, 2, 8, 9, 3, 6, 3, 4, 4, 5, 8])
b = np.array([2, 1, 1, 9, 9, 9, 7, 5, 4, 8, 6, 5, 4, 4, 7, 2, 1, 1, 9, 9])
和 2 个常数:
c = 6
d = 3
a我每次从数组中提取一个数组:
中的元素
a小于c,连续超过 2 次,和中的元素连续
b小于d, 大于 2 次。
为此,我使用以下代码:
mask = ~((a < c) & (b < d))
split_indices = np.where(mask)[0]
for subarray in np.split(a, split_indices + 1):
if len(subarray) > 2:
x=(subarray[:-1])
print(x)
现在我需要计算每次满足我的条件时的累计和a - x。为此,我刚刚在最后一个if循环中添加了以下代码:
xx = np.cumsum(sum(c) - x))
哪个输出13和7
我的问题是我需要知道所有xx. 因此,对于这个最小示例,结果值将是10. 如果我将命令np.mean(xx)放在循环内部甚至外部,它只会计算最后一个xx值的平均值,而不是循环中计算的每次的平均值xx。有谁知道如何实现这一目标?