Numpy ndarray.itemset()函数| Python
numpy.ndarray.itemset()
函数将标量插入数组。
必须至少有 1 个参数,并将最后一个参数定义为 item。然后, arr.itemset(*args) 等价于但比 arr[args] = item 更快。该项目应该是一个标量值,并且 args 必须选择数组 arr 中的单个项目。
Syntax : numpy.ndarray.itemset(*args)
Parameters :
*args : If one argument: a scalar, only used in case arr is of size 1. If two arguments: the last argument is the value to be set and must be a scalar, the first argument specifies a single array element location. It is either an int or a tuple.
代码#1:
# Python program explaining
# numpy.ndarray.itemset() function
# importing numpy as geek
import numpy as geek
geek.random.seed(345)
arr = geek.random.randint(9, size =(3, 3))
print("Input array : ", arr)
arr.itemset(4, 0)
print ("Output array : ", arr)
输出 :
Input array : [[8 0 3]
[8 4 3]
[4 1 7]]
Output array : [[8 0 3]
[8 0 3]
[4 1 7]]
代码#2:
# Python program explaining
# numpy.ndarray.itemset() function
# importing numpy as geek
import numpy as geek
geek.random.seed(345)
arr = geek.random.randint(9, size =(3, 3))
print("Input array : ", arr)
arr.itemset((2, 2), 9)
print ("Output array : ", arr)
输出 :
Input array : [[8 0 3]
[8 4 3]
[4 1 7]]
Output array : [[8 0 3]
[8 4 3]
[4 1 9]]