I'm looking for a numpy function that will do the equivalent of:
indices = set([1, 4, 5, 6, 7])
zero = numpy.zeros(10)
for i in indices:
zero[i] = 42
We can assign new values to an element of a NumPy array using the = operator, just like regular python lists. A few examples are below (note that this is all one code block, which means that the element assignments are carried forward from step to step).
To select an element from Numpy Array , we can use [] operator i.e. It will return the element at given index only.
You can access an array element by referring to its index number. The indexes in NumPy arrays start with 0, meaning that the first element has index 0, and the second has index 1 etc.
Instead, go for something like: ... [2] => stdClass Object ( [qty] => 2 [id] => 2 [name] => Single 1 Tag Innenraum 2 [one] => Array( [name] => jiya [sirname] => rathod ) [two] => Array( [name] => jiya [sirname] => rathod ) ...
You can just give it a list of indices:
indices = [1, 4, 5, 6, 7]
zero = numpy.zeros(10)
zero[indices] = 42
If you have an ndarray:
>>> x = np.zeros((3, 3, 3))
>>> y = [0, 9, 18]
>>> x
array([[[ 0., 0., 0.],
[ 0., 0., 0.],
[ 0., 0., 0.]],
[[ 0., 0., 0.],
[ 0., 0., 0.],
[ 0., 0., 0.]],
[[ 0., 0., 0.],
[ 0., 0., 0.],
[ 0., 0., 0.]]])
>>> np.put(x, y, 1)
>>> x
array([[[ 1., 0., 0.],
[ 0., 0., 0.],
[ 0., 0., 0.]],
[[ 1., 0., 0.],
[ 0., 0., 0.],
[ 0., 0., 0.]],
[[ 1., 0., 0.],
[ 0., 0., 0.],
[ 0., 0., 0.]]])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With