Python noobie在这里:)试图弄清楚如何简洁地做事.
>>> f = np.zeros((2,2), dtype=int)
>>> f[0][1] = 100
>>> f[1][0] = 200
>>> print f
[[ 0 100]
[200 0]]
>>> f1 = # do something special
>>> print f1
[[ 0 101]
[201 0]]
Run Code Online (Sandbox Code Playgroud)
谢谢!
稍微好一点的方法是使用布尔索引:
>>> import numpy as np
>>> a = np.array([[0,100],[200,0]])
>>> a[a!=0] += 1
>>> a
array([[ 0, 101],
[201, 0]])
Run Code Online (Sandbox Code Playgroud)
它是一个改进的原因在Matlab论坛上得到了很好的解释,其中find起到了类似的作用np.where.例如,在这个Matlab线程中,提到了find使用布尔数组调用.find然后执行一些额外的功能来提取索引.然后使用这些索引在numpy中执行所谓的"花式索引".但是,数组本身可以使用布尔数组以这种奇特的方式编入索引.