如果满足条件,则从列表中存储项目

时间:2015-09-10 11:56:55

标签: python

Python不会给我任何错误,但也不是我想要的结果(因此我也未能在论坛中找到帮助)。 我想获得两个新的数组F_h和F_n。如果列表data_WL中的项目是 如果大于0.5,我希望将F_n作为计数器,将F_h存储为大于0.5的值。总共9年我想拥有3年的窗户。

data_WL = [0.4001, 0.3966472, 0.4365047, 0.4950109, 0.5348455, 0.5816008, 0.5816009, 0.09, -0.03]
one_year = 3
all_years= 9
data_WL1 = [float(x) for x in data_WL] # convert items to float
print data_WL1

F_h = []
F_n = []
for i in arange(0, all_years - 1 ,1):
    flood = 0
    F_h.append(data_WL[i])

    for j in arange(0, one_year -1,1):
        if data_WL1[j] >= 0.5:
            flood = flood + 1
            F_h.append(float(data_WL1[j])) 
        else:
            flood = flood
        print flood

    F_n.append(flood)

print "Flood frequency"
print F_n
print "Flood magnitude"
print F_h   

因此结果应如下:

Flood Frequency
[0,2,1]

Flood magnitude
[0.5348455, 0.5816008, 0.5816009]

1 个答案:

答案 0 :(得分:0)

根据我的理解,这可能会给你你想要的东西

>>> [x for x in enumerate(data_WL) if x[1] > 0.5]
[(4, 0.5348455), (5, 0.5816008), (6, 0.5816009)]
>>> F_n, F_h = zip(*[x for x in enumerate(data_WL) if x[1] > 0.5])
>>> F_n
(4, 5, 6)
>>> F_h
(0.5348455, 0.5816008, 0.5816009)
相关问题