我正在寻找一个优雅的解决方案:
data = np.loadtxt(file)
# data[:,0] is a time
# data[:,1] is what I want to extract
mean = 0.0
count = 0
for n in xrange(np.size(data[:,0])):
if data[n,0] >= tstart and data[n,0] <= tend:
mean = mean + data[n,1]
count = count + 1
mean = mean / float(count)
我猜我也可以先提取我的 2D 数组然后应用np.mean
它,但我觉得可能有一些列表理解的优点可以使它更优雅(我来自 FORTRAN 背景......)。我在想类似的东西(显然是错误的,因为i
它不是索引):
np.mean([x for x in data[i,1] for i in data[:,0] if i >= tstart and i <= tend])