Convert an array with numpy buffer to int with separate values

I read most of How to convert a boolean array to an int array , but I still lost how (most efficiently) to convert a numpybool to an int array, but with different values. For example, I have:

>>> k=np.array([True, False, False, True, False])
>>> print k
[ True False False  True False]

I would like this to be converted to an array where True says 2 and False - 5.

Of course, I can always establish a linear equation:

>>> print 5-k*3
[2 5 5 2 5]

... which, being vectorized, uselessly uses both addition (subtraction) and multiplication; furthermore, if I need the opposite values ​​(5 for True and 2 for False), I basically have to use (and recount) another equation:

>>> print 2+k*3
[5 2 2 5 2]

... for me it is a reading problem.

, /, numpy. ?

+4
2

, numpy.where - , :

>>> import numpy as np
>>> k = np.array([True, False, False, True, False])
>>> np.where(k, 2, 5)
array([2, 5, 5, 2, 5])
+7

, , np.array ( Python!), :

>>> z=np.array([2,5])
>>> print z
[2 5]

... (k) int " " (z):

>>> print z[k.astype(int)]
[5 2 2 5 2]

[2,5] [5,2], .

, ( , () k.asdistinctvalues([2,5]) - ?)

+1

All Articles