繁体   English   中英

遍历2d numpy数组,直到仅包含某些特定值

[英]Iterate over a 2d numpy array until it contains only some specific values

我有一个二维的numpy数组,其中每个单元格中包含浮点数。

我想遍历它并更改每个单元格的值(如果满足特定条件),直到每个单元格中仅包含值1-1NaN为止。

我该如何实现?

在numpy中,您可以使用条件索引。 即:

import numpy as np
x = np.arange(10)
c =  x > 5 
print c

会给

array([False, False, False, False, False, False,  True,  True,  True,  
True], dtype=bool)

最后使用条件

x[c] = -1
print x

给出array([ 0, 1, 2, 3, 4, 5, -1, -1, -1, -1])

暂无
暂无

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

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