简体   繁体   English

python遍历指定的数组元素

[英]python looping over specified array elements

I am using numpy.ndenumerate for array element iteration: 我正在使用numpy.ndenumerate进行数组元素迭代:

a = np.ones((10,10,10))
for (x,y,z), value in numpy.ndenumerate(a):
    do stuff with x,y,z and value

How can I iterate only over specified element values? 如何仅对指定的元素值进行迭代? For example if the array is binary, how do I iterate over all ones? 例如,如果数组是二进制的,那么如何遍历所有数组?

Currently I am using something like: 目前,我正在使用类似:

specified = np.nonzero(a) #or np.where(a == some_value)
for i in xrange(np.count_nonzero(a)):
    do stuff with x,y,z = specified[0][i], specified[1][i], specified[2][i] 
    and value = a[x,y,z]

This is obviously pretty ugly and I am shure there is a nice short way. 这显然很丑陋,我确信这是一个不错的捷径。

I don't see anything wrong with np.nonzero(a) . 我看不到np.nonzero(a)有什么问题。 However, what's returned from np.nonzero is a tuple of elements where each element in the tuple is the indices that are non-zero for a particular dimension. 但是,从np.nonzero返回的是一个元素元组,其中该元组中的每个元素都是特定维度的非零索引。 Specifically, the first element of the tuple are the row locations of what is non-zero, the second element is the column locations of what is non-zero, the third element is the slice locations of what is non-zero, etc. Therefore, for each grouping of elements in the same index for each tuple element, you get a group of coordinates that tell you which locations are non-zero in your array. 具体来说,元组的第一个元素是非零的行位置,第二个元素是非零的列位置,第三个元素是非零的切片位置,依此类推。 ,对于每个元组元素在同一索引中的每个元素分组,您都会得到一组坐标,这些坐标告诉您数组中哪些位置非零。

Once you obtain these, you can simply zip over them and extract your values: 一旦获得这些,就可以简单地将它们zip并提取值:

specified = np.nonzero(a)
for (x,y,z) in zip(*specified):
     # do your stuff with a[x,y,z] and x,y,z...
     value = a[x,y,z]
     #....
     #....

The above assumes that specified has three tuple elements corresponding to a being three dimensional. 上述假设specified有对应于三个元组元素a是三维的。 At each iteration of the loop, we grab unique (x,y,z) triplets that correspond to non-zero entries in a . 在循环的每次迭代中,我们抓住独特(x,y,z)对应于在非零项三元组a You can go ahead and do your processing on each triplet. 您可以继续进行每个三元组的处理。


However, the above code assumes that you're going to do something with both the (x,y,z) triplets and the values themselves within the same for loop body. 但是,以上代码假定您将对(x,y,z)三元组和相同的for循环体内的值本身for If it is your intention to extract out the values only, then Boolean indexing is the better way to go. 如果仅打算提取值,那么布尔索引是更好的选择。 Specifically, you can just do something like this: 具体来说,您可以执行以下操作:

values = a == some_value

values will contain a Boolean array of the same size as a that indicates where a value is equal or not equal to some_value . values将包含相同的尺寸的一个布尔阵列a表示,其中的值是等于或不等于some_value From there, you can perhaps do something like this: 从那里,您可以执行以下操作:

a[values] = some_other_value

This replaces all values in a that equal to some_value with some_other_value . 这将用some_other_value替换等于some_value a中的所有值。 If it's your intention to simply access values and do some manipulation on these values, then the above is the better way to go. 如果您只是想访问值并对这些值进行一些操作,那么上面的方法是更好的选择。 However, I can't say that for sure as I don't know what "stuff" you're intending to do inside your for loop body. 但是,我不能肯定地说,因为我不知道您打算在for循环体内执行什么“工作”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM