add a number to all odd or even indexed elements in numpy array without loops

前端 未结 4 1225
后悔当初
后悔当初 2021-02-07 14:30

Lets say your numpy array is:

 A =    [1,1,2,3,4]

You can simply do:

A + .1

to add a number to tha

4条回答
  •  再見小時候
    2021-02-07 15:15

    If the list didn't start with two 1 and you wanted to add to all even numbers, you could use:

    A[1::2] += 0.1
    

    or

    A[::-2][::-1] += 0.1
    

    In the latter case, [::-1] is used to reverse the array back to normal order.

提交回复
热议问题