numpy.put#

numpy.put(a,ind,v,mode='raise')[source]#

Replaces specified elements of an array with given values.

The indexing works on the flattened target array.put is roughlyequivalent to:

a.flat[ind]=v
Parameters:
andarray

Target array.

indarray_like

Target indices, interpreted as integers.

varray_like

Values to place ina at target indices. Ifv is shorter thanind it will be repeated as necessary.

mode{‘raise’, ‘wrap’, ‘clip’}, optional

Specifies how out-of-bounds indices will behave.

  • ‘raise’ – raise an error (default)

  • ‘wrap’ – wrap around

  • ‘clip’ – clip to the range

‘clip’ mode means that all indices that are too large are replacedby the index that addresses the last element along that axis. Notethat this disables indexing with negative numbers. In ‘raise’ mode,if an exception occurs the target array may still be modified.

See also

putmask,place
put_along_axis

Put elements by matching the array and the index arrays

Examples

>>>importnumpyasnp>>>a=np.arange(5)>>>np.put(a,[0,2],[-44,-55])>>>aarray([-44,   1, -55,   3,   4])
>>>a=np.arange(5)>>>np.put(a,22,-5,mode='clip')>>>aarray([ 0,  1,  2,  3, -5])
On this page