【发布时间】:2013-11-12 15:18:27
【问题描述】:
我有一个包含四列的随机时间序列数据,例如:年、月、日、降水。我想计算不同拼写长度的干/湿拼写。我正在寻找一种更方便的方法来做到这一点,而目前正在使用如下一些丑陋的代码:
import numpy as np
data = np.loadtxt('Data Series.txt', usecols=(1,3))
dry = np.zeros(12)
wet = np.zeros(12)
rows,cols = data.shape #reading number of rows and columns into variables
for i in xrange (0,rows):
for m in xrange(0,12):
if data[i,1] == 0 and data[i-1,1] == 0 and data[i-2,1] == 0:
if data[i,0] == m+1:
dry[m] += 1.0
if data[i,1] > 0 and data[i-1,1] > 0 and data[i-2,1] > 0:
if data[i,0] == m+1:
wet[m] += 1.0
print '3 Days Dry Spell\n', dry
print '3 Days Wet Spell\n', wet
现在,如果我想计算 4、5、6 天的咒语,那么“如果 data[i,1] == 0 and data[i-1,1] == 0.....” 变成一个巨大的。任何人都可以帮助我,以便我可以只给出拼写长度而不是这条长而丑陋的线吗?
【问题讨论】:
-
仅在列表列表中包含复杂数据通常是个坏主意。这是一个这样的例子。这里有趣的数据都在一大组列表的第二项中。这使得处理起来很棘手。我要么从一开始就提取我想要的数据,要么将其放入对象中。
-
您似乎正在尝试计算干旱指数。检查 [journals.ametsoc.org/doi/abs/10.1175/…paper) 比较一些众所周知的索引。如果您有时间特别关注帕尔默指数,以及他如何定义干湿期
标签: python arrays for-loop iteration time-series