Suppose that you have a 1D numpy array containing elements that are either 3 or 4
假设您有一个包含3或4个元素的numpy数组
for example:
例如:
3
4
4
4
3
3
3
I would like to alter this array so that if an element is 3 then it should become some number X and if the element is 4 it should become some number Y. It is guaranteed that X is different than Y. For the above array we would get:
我想改变这个数组,这样如果一个元素是3,那么它应该变成一个数字X,如果元素是4,它应该变成一个数字Y.保证X不同于Y.对于上面的数组我们会得到:
X
Y
Y
Y
X
X
X
I was thinking about doing something like this:
我在考虑做这样的事情:
arr[arr==3]=X
arr[arr==4]=Y
however, what if X is 4? Then in the end the entire array will contain just Ys.
但是,如果X是4怎么办?然后最后整个数组将只包含Ys。
I am trying to avoid using a for loop for performance reasons, but if it's the only way to go, I can afford following that route.
我试图避免出于性能原因使用for循环,但如果这是唯一的方法,我可以负担得起这条路线。
2 个解决方案
#1
6
Since the input 1D array contains only 3
or 4
, you can use np.where
, like so -
由于输入1D数组只包含3或4,你可以使用np.where,就像这样 -
np.where(A==3,X,Y)
Sample run #1 -
样品运行#1 -
In [55]: A
Out[55]: array([4, 3, 4, 3, 3, 3, 4, 4, 4, 4, 3, 3, 4, 3, 4])
In [56]: X = 30; Y = 40
In [57]: np.where(A==3,X,Y)
Out[57]: array([40, 30, 40, 30, 30, 30, 40, 40, 40, 40, 30, 30, 40, 30, 40])
Sample run #2 (if X
is 4
) -
样品运行#2(如果X为4) -
In [60]: A
Out[60]: array([4, 3, 4, 3, 3, 3, 4, 4, 4, 4, 3, 3, 4, 3, 4])
In [61]: X = 4; Y = 40
In [62]: np.where(A==3,X,Y)
Out[62]: array([40, 4, 40, 4, 4, 4, 40, 40, 40, 40, 4, 4, 40, 4, 40])
#2
1
This is a typical use for the map function. A way of implementing it would be:
这是地图功能的典型用法。实现它的方法是:
result = map(lambda k: X if k==3 else Y, myarray)
result = map(lambda k:X if k == 3 else Y,myarray)
You can replace k == 3
with any function that returns a boolean like so:
你可以用任何返回boolean的函数替换k == 3,如下所示:
result = map(lambda k: X if myfun(k) else Y, myarray)
result = map(lambda k:X if myfun(k)else Y,myarray)
You may want to consider this post, however you will not get much speed up.
你可能想要考虑这篇文章,但是你不会加快速度。
#1
6
Since the input 1D array contains only 3
or 4
, you can use np.where
, like so -
由于输入1D数组只包含3或4,你可以使用np.where,就像这样 -
np.where(A==3,X,Y)
Sample run #1 -
样品运行#1 -
In [55]: A
Out[55]: array([4, 3, 4, 3, 3, 3, 4, 4, 4, 4, 3, 3, 4, 3, 4])
In [56]: X = 30; Y = 40
In [57]: np.where(A==3,X,Y)
Out[57]: array([40, 30, 40, 30, 30, 30, 40, 40, 40, 40, 30, 30, 40, 30, 40])
Sample run #2 (if X
is 4
) -
样品运行#2(如果X为4) -
In [60]: A
Out[60]: array([4, 3, 4, 3, 3, 3, 4, 4, 4, 4, 3, 3, 4, 3, 4])
In [61]: X = 4; Y = 40
In [62]: np.where(A==3,X,Y)
Out[62]: array([40, 4, 40, 4, 4, 4, 40, 40, 40, 40, 4, 4, 40, 4, 40])
#2
1
This is a typical use for the map function. A way of implementing it would be:
这是地图功能的典型用法。实现它的方法是:
result = map(lambda k: X if k==3 else Y, myarray)
result = map(lambda k:X if k == 3 else Y,myarray)
You can replace k == 3
with any function that returns a boolean like so:
你可以用任何返回boolean的函数替换k == 3,如下所示:
result = map(lambda k: X if myfun(k) else Y, myarray)
result = map(lambda k:X if myfun(k)else Y,myarray)
You may want to consider this post, however you will not get much speed up.
你可能想要考虑这篇文章,但是你不会加快速度。