Insert scalar into an array (scalar is cast to array’s dtype, if possible)
There must be at least 1 argument, and define the last argument as item. Then,
a.itemset(*args) is equivalent to but faster than
a[args] = item. The item should be a scalar value and
args must select a single item in the array
Compared to indexing syntax,
itemset provides some speed increase for placing a scalar into a particular location in an
ndarray, if you must do this. However, generally this is discouraged: among other problems, it complicates the appearance of the code. Also, when using
item) inside a loop, be sure to assign the methods to a local variable to avoid the attribute look-up at each loop iteration.
>>> np.random.seed(123) >>> x = np.random.randint(9, size=(3, 3)) >>> x array([[2, 2, 6], [1, 3, 6], [1, 0, 1]]) >>> x.itemset(4, 0) >>> x.itemset((2, 2), 9) >>> x array([[2, 2, 6], [1, 0, 6], [1, 0, 9]])
© 2005–2019 NumPy Developers
Licensed under the 3-clause BSD License.