0

i want to extract every 4th element in the numpy array

The array is

a = [0,1,3,4,5,6,7,8, 9]

final_array shuld be [0,5,9]

The array is ofocurse very huge, so just the indices (0,4,8) would not be possible i guess.

Is there a quicker way to do it instead of iterating using for loop.

infoclogged
  • 3,641
  • 5
  • 32
  • 53
  • 2
    `a[::4]`. Cf, say, [this](https://stackoverflow.com/questions/43093495/how-can-i-slice-a-list-to-get-every-2-consecutive-elements-of-the-list-in-new-li). – keepAlive Apr 26 '22 at 20:44

1 Answers1

0

You can use numpy's slicing, simply start:stop:step.

>>> xs
array([1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4])
>>> xs[1::4]
array([2, 2, 2])

This creates a view of the the original data, so it's constant time. It'll also reflect changes to the original array and keep the whole original array in memory:

>>> a
array([1, 2, 3, 4, 5])
>>> b = a[::2]         # O(1), constant time
>>> b[:] = 0           # modifying the view changes original array
>>> a                  # original array is modified
array([0, 2, 0, 4, 0])

so if either of the above things are a problem, you can make a copy explicitly:

>>> a
array([1, 2, 3, 4, 5])
>>> b = a[::2].copy()  # explicit copy, O(n)
>>> b[:] = 0           # modifying the copy
>>> a                  # original is intact
array([1, 2, 3, 4, 5])

This isn't constant time, but the result isn't tied to the original array. The copy also contiguous in memory, which can make some operations on it faster.