Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在数组中查找特定值_Python_Arrays_Numpy - Fatal编程技术网

Python 在数组中查找特定值

Python 在数组中查找特定值,python,arrays,numpy,Python,Arrays,Numpy,我试图将数组中的条件值插入到空数组中的精确位置 empty_array = np.zeros([40,100]) for x in range (1,24): array[x,:,:] #which is also sized 40x100 if the_values_in_the_array < 0.25: the_values_in_the_array = 0 empty_array = empty_array + array [x,:,:]

我试图将数组中的条件值插入到空数组中的精确位置

empty_array = np.zeros([40,100])
for x in range (1,24):
    array[x,:,:] #which is also sized 40x100
    if the_values_in_the_array < 0.25:
         the_values_in_the_array = 0
    empty_array = empty_array + array [x,:,:]
空数组=np.zero([40100])
对于范围(1,24)内的x:
阵列[x,:,:]#大小也为40x100
如果\u数组中的\u值<0.25:
_数组中的_值为0
空数组=空数组+数组[x,:,:]
这个逻辑应该使用哪种语法?我应该如何扫描\u数组中的\u值以找到条件值?

empty\u array=np.zero([40100])
empty_array = np.zeros([40,100])
array = np.random.rand(24,40,100)

array[array<0.25]=0 # change all the values which is <0.25 to 0
for x in range(1,24):
    empty_array += array[x,:,:]
数组=np.random.rand(24,40100)
array[array我认为这是您正在尝试执行的操作。我建议使用
np.where
例程将值设置为小于0.25到零。然后您可以仅对数组的第一个维度求和,以获得您正在寻找的输出数组。我在示例中减少了问题的维度

import numpy as np

vals = np.random.random([24, 2, 3])
vals_filtered = np.where(vals < 0.25, 0.0, vals)
out = vals_filtered.sum(axis=0)
print("First sample array has the slice vals[0,:,:]:\n{}\n".format(vals[0, :, :]))
print("First sample array with vals>0.25 set to 0.0:\n{}\n".format(vals_filtered[0, :, :]))
print("Output array is the sum over the first dimension:\n{}\n".format(out))

这就是您要找的计算吗?调用
vals.sum(axis=0)
是一种更快的操作方法。通常最好调用numpy的内置数组例程,而不是显式
for
循环。

@F L,您的
数组的维数是多少,您说它是40*100,为什么要使用
数组[x,:,:]
,看起来像是三维的?@MaThMaX是的,x基本上是x的倍。但是大小是40*100。将代码简化为一个简单的示例,可以独立于外部定义运行会很有帮助。这里我们必须假设你所说的
array
数组中的\u值。\u
。非常感谢。非常感谢比我想象的要简单。@FL,很高兴能帮上忙。有关这就是我的问题的更多信息。谢谢你。谢谢你,现在我知道了另一种新语法(和新逻辑)来解决像我这样的简单问题。很好的信息。
First sample array has the slice vals[0, :, :]: 
[[ 0.16272567  0.13695067  0.5954866 ]
 [ 0.50367823  0.8519252   0.3000367 ]]

First sample array with vals>0.25 set to 0.0: 
[[ 0.          0.          0.5954866 ]
 [ 0.50367823  0.8519252   0.3000367 ]]

Output array is the sum over the first dimension: 
[[ 11.12707813  12.04175706  11.5812803 ]
 [ 13.73036272   9.3988165   12.41808745]]