1

I have an array like:

a=np.array([20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
 68 69 70 71 72 73 74 75 76 77 78 79])

requirement: I would to like to access 10 indices in an array the above array length is 60,60/10=6. So, i need every 6th indices in an array a.

required output:[0,6,12,18,24,30,36,42,48,64,60]

Dani Mesejo
  • 61,499
  • 6
  • 49
  • 76
Bhar Jay
  • 184
  • 8
  • Similar https://stackoverflow.com/questions/6683690/making-a-list-of-evenly-spaced-numbers-in-a-certain-range-in-python – Arunesh Singh Jul 26 '22 at 10:59

3 Answers3

1

Numpy is powerful i would recommend to read the Documentation about indexing in numpy

everySixthEntry=a[np.arange(0,a.shape[0],6)]

jack
  • 281
  • 1
  • 7
  • your anwers is so helpfull to me. But i need index values instead of element values – Bhar Jay Jul 26 '22 at 11:45
  • You can also access the entries directly with: `everySixthEntry=a[::6]` which will be way faster. `%timeit -n 3 -r 3 a[np.arange(0,a.shape[0],6)] # 31.7 µs ± 29 µs per loop` vs. `%timeit -n 30 -r 3 a[::6] # 220 ns ± 57.9 ns per loop` – Andrew Jul 27 '22 at 12:10
1

You can generate the indexes for any array a with np.arange(len(a)). To access every 6th index use the a slice a[start:stop:step]. Jack posted one way, here a bit more detailed.

import numpy as np

# define your data. a = [20, ..., 79]
a = np.arange(60) + 20

# generate indexes for the array, index start at 0 till len(a)-1
indexes = np.arange(len(a))

# reduce the indexes to every 6th index
indexes = indexes[::6]  # [start:stop:step]
print(indexes)
# -> array([ 0,  6, 12, 18, 24, 30, 36, 42, 48, 54])
# 60 isn't included as the array is only 59 long

The same result a bit different. You can also use np.arange steps.

# the same result a bit different
indexes = np.arange(0, len(a), 6)  # (start,stop,step)
print(indexes)
# -> array([ 0,  6, 12, 18, 24, 30, 36, 42, 48, 54])

and in case you want to access the values of your original array

print(a[indexes])
# -> array([20, 26, 32, 38, 44, 50, 56, 62, 68, 74])

Basics of slicing

a[start:stop:step] is equivalent to a[slice(start, stop, step)]. If you don't want to specify any of start, stop, step set it to None. start and stop takes values from 0 to len(a)-1 and negative represents the position from the end of the array.

Some Slice Examples:

step = 20
a[slice(None, None, step)], a[slice(0, -1, step)], a[0: -1: step], a[::step]
# all -> array([20, 40, 60])

# the first 4 elements
step = 1
start = 0  # or None
end = 5
a[slice(start, end, step)], a[slice(start, end)] , a[start: end: step] , a[start:end]
# all -> array([20, 21, 22, 23])

# the last 4 elements
step = 1
start = -4
end = None  # -1 will cute the last entry
a[slice(start, end, step)], a[slice(start, end)] , a[start: end: step] , a[start:end]
# all -> array([76, 77, 78, 79]
Andrew
  • 817
  • 4
  • 9
0

I think you meant to say: The required index values are [0,6,12,18,24,30,36,42,48,64,60] Corresponding output values are [20, 26, 32, 38, 44, 50, 56, 62, 68, 74]

The code below should give you the values for every 6th index.

a=np.array([20,21,22,23,24,25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43,
 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67,
 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,])

Out=[]
for i in range(10):
    Out.append(a[6*i])
print(Out)

Output is :

[20, 26, 32, 38, 44, 50, 56, 62, 68, 74]

If the Index values are required: Do the following

Out1=[]
for i in range(0,11):  #for only 10 indices (going from 0 to 10)
    print(6*i)
    Out1.append(6*i)
print("The required index values is : {}".format(Out1))

This gives an output :

0
6
12
18
24
30
36
42
48
54
60
The required index values is : [0, 6, 12, 18, 24, 30, 36, 42, 48, 54, 60]
Geg
  • 19
  • 4