📜  Python中的 numpy.put()

📅  最后修改于: 2022-05-13 01:54:34.024000             🧑  作者: Mango

Python中的 numpy.put()

numpy.put()函数用给定的 p_array 值替换数组的特定元素。数组索引适用于扁平数组。

Syntax: numpy.put(array, indices, p_array, mode = 'raise')

参数 :

array   : array_like, target array
indices : index of the values to be fetched
p_array : array_like, values to be placed in target array
mode    : [{‘raise’, ‘wrap’, ‘clip’}, optional] mentions how out-of-bound indices will behave
                  raise : [default]raise an error 
                  wrap  : wrap around
                  clip  : clip to the range

Python
# Python Program explaining
# numpy.put()
 
import numpy as geek
 
a = geek.arange(5)
geek.put(a, [0, 2], [-44, -55])
print("After put : \n", a)


Python
# Python Program explaining
# numpy.put()
 
import numpy as geek
 
a = geek.arange(5)
geek.put(a, 22, -5, mode='clip')
print("After put : \n", a)


输出 :

After put : 
[-44,   1, -55,   3,   4]

Python

# Python Program explaining
# numpy.put()
 
import numpy as geek
 
a = geek.arange(5)
geek.put(a, 22, -5, mode='clip')
print("After put : \n", a)

输出 :

array([ 0,  1,  2,  3, -5])

代码来源:
https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.put.html#numpy.put
笔记 :
这些代码不会在在线 IDE 上运行。因此,请在您的系统上运行它们以探索其工作原理。