Python错误代码

时间:2015-02-24 17:55:00

标签: python numpy

所以我试图平均每600行。我得到一个超出范围的Python错误索引,数字不正确。帮助吗

import numpy as np

data = np.loadtxt('Y:/Canopy/PSUone.txt', delimiter = ',', skiprows = 0)
shape = np.shape(data)
data_you_want = data[:,3]
daily_averages = list()

for i,d in enumerate(data_you_want):
    if (i % 600) == 0:
        avg_for_day = np.mean(data_you_want[i - 600:i])
        daily_averages.append(avg_for_day)

with open('Y:/Canopy/newPSUone.txt', 'w') as f:
    f.write('Wind U Direction\n')
    f.write('------------------\n')
    for i, d in enumerate(data_you_want):
         s = '{0:4.3f}\n'. format(daily_averages[i])
         f.write(s)

f.close()

1 个答案:

答案 0 :(得分:0)

我认为你遇到的问题是:

for i,d in enumerate(data_you_want):
    if (i % 600) == 0:
        avg_for_day = np.mean(data_you_want[i - 600:i])
        daily_averages.append(avg_for_day)

......紧随其后:

for i, d in enumerate(data_you_want):
     s = '{0:4.3f}\n'. format(daily_averages[i])
     f.write(s)

因为您只是从data_you_want到平均值daily_averages的第600行,所以len(data_you_want) // 600中将包含daily_averages个元素。如果您迭代data_you_want并尝试daily_average[i],那么您将达到i大于daily_averages长度的点,您将获得错误。