所以我有两个相当大的列表(两者中有3636586个值),我试图使用np.where()来查找使用这两个列表满足两个条件的值的数量。列表是
cat1 = [ 0.01027171 0.01691939 0.01614619 ..., 10.18688393 10.21286678
10.20675182]
cat2 = [ 4.81194684e-09 5.13640614e-08 5.48405854e-09 ..., 2.95787549e-06
3.43822015e-09 2.17239404e-09]
然后我使用np.where():
summ = np.array([])
summ = np.append(summ, np.where((cat1 > 6.) & (cat2*1000 > .83)))
print(summ)
>>>[ 3615964. 3616643. 3616732. 3617188. 3618296. 3618976. 3619040.
3619205. 3619429. 3620039. 3620142. 3620185. 3620487. 3620810.
3621045. 3621375. 3621396. 3621828. 3622030. 3622326. 3622713.
3622999. 3623137. 3623202. 3624357. 3624919. 3625077. 3626185.
3626558. 3626666. 3627142. 3627279. 3627660. 3628004. 3628558.
3629997. 3630053. 3630128. 3630801. 3631271. 3631567. 3632210.
3632269. 3632285. 3632589. 3633107. 3633816. 3634833. 3635190.
3635307. 3635608. 3635711. 3635767. 3636159. 3636227.]
所以我知道这些是满足条件的值的索引,我可以调用len(summ)来查找总量,即55.但是,我的问题是我想稍后在循环中使用它(这就是为什么在开头有np.append()无意义的原因,我只是为时尚早,并且找到len(summ)只是给出了所有循环的索引总数。无论如何我能找到每个循环的索引总数吗?如果我尝试找到np.where()列表的长度,我得到这个:
summ = np.array([])
summ = np.append(summ, len(np.where((cat1 > 6.) & (cat2*1000 > .83))))
print(summ)
>>>[ 1.]
我希望它输出55.这样,当我把它放在循环中时,它将输出满足条件的索引计数,而不是仅输出索引本身而没有分离。我希望它看起来像这样:
sample_array = [.39, .29, .12, ...] #array of some length
summ = np.array([])
for i in sample_array:
summ = np.append(summ, len(np.where((cat1 > 6.) & (cat2*1000 > i)))) #len() currently doesn't work like how I want it to
print(summ)
>>> [55, 23, 45, ...] #array of the count of indices for each loop that satisfy the conditions
当我像这样运行时,我得到一个1的列表的输出,就像它在上面表现的那样。我已经尝试过sum(np.where(...)),但是当我刚打印summ时,它只是输出索引。