In versions 1.4 and above, the numpy function provides the in1d
function.
>>> test = np.array([0, 1, 2, 5, 0]) >>> states = [0, 2] >>> np.in1d(test, states) array([ True, False, True, False, True], dtype=bool)
You can use this as a mask for assignment.
>>> test[np.in1d(test, states)] = 1 >>> test array([1, 1, 1, 5, 1])
Here are some more complex uses of the numpy indexing and assignment syntax that I think apply to your problem. Note the use of bitwise operators to replace if
based logic:
>>> numpy_array = numpy.arange(9).reshape((3, 3)) >>> confused_array = numpy.arange(9).reshape((3, 3)) % 2 >>> mask = numpy.in1d(numpy_array, repeat_set).reshape(numpy_array.shape) >>> mask array([[False, False, False], [ True, False, True], [ True, False, True]], dtype=bool) >>> ~mask array([[ True, True, True], [False, True, False], [False, True, False]], dtype=bool) >>> numpy_array == 0 array([[ True, False, False], [False, False, False], [False, False, False]], dtype=bool) >>> numpy_array != 0 array([[False, True, True], [ True, True, True], [ True, True, True]], dtype=bool) >>> confused_array[mask] = 1 >>> confused_array[~mask & (numpy_array == 0)] = 0 >>> confused_array[~mask & (numpy_array != 0)] = 2 >>> confused_array array([[0, 2, 2], [1, 2, 1], [1, 2, 1]])
Another approach would be to use numpy.where
, which creates a new array using the values โโfrom the second argument, where mask
is true and the values โโfrom the third argument, where mask
is false. (As with the assignment, the argument can be a scalar or an array of the same shape as mask
.) It can be a little more efficient than the above, and it is certainly more concise:
>>> numpy.where(mask, 1, numpy.where(numpy_array == 0, 0, 2)) array([[0, 2, 2], [1, 2, 1], [1, 2, 1]])