2

If I have an array that is 100 elements in length, what is the most Pythonic way to get every n indices. For example, if I wanted every 5 indices of an array a, how could I get an array b=[[0,1,2,3,4],[5,6,7,8,9],[10,11,12,13,14],...], where each element of b is a sub-array of every 5 indices?

2
  • a.reshape(-1,5) or np.reshape(a, (-1, 5)) Commented Oct 9, 2017 at 18:31
  • 1
    No, list doesn't have a shape. Commented Oct 9, 2017 at 18:32

3 Answers 3

5

You simply want to reshape your array:

>>> arr = np.arange(100)
>>> arr
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
       17, 18, 19, 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, 80, 81, 82, 83, 84,
       85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99])
>>> arr.reshape(-1, 5)
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19],
       [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],
       [80, 81, 82, 83, 84],
       [85, 86, 87, 88, 89],
       [90, 91, 92, 93, 94],
       [95, 96, 97, 98, 99]])

Note, I used -1 on the first axis, numpy is smart enough to "solve the equation" as long as you give it every other axis explicitly. You could have, of course, done this completely explicitly:

>>> arr.reshape(20, 5)
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19],
       [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],
       [80, 81, 82, 83, 84],
       [85, 86, 87, 88, 89],
       [90, 91, 92, 93, 94],
       [95, 96, 97, 98, 99]])
Sign up to request clarification or add additional context in comments.

2 Comments

what if I wanted to get every n = odd index from an even length array. For example if I wanted every 3 indices in an array of length 100?
@maelstromscientist using slicing: arr[::3]
2

Update:

If you are using lists, a very pythonic way to do it:

size = 5 # the number of elements of each sublists
l = list(range(100))
result = [l[step:step + size] for step in range(0, len(l), size)]

Ouput:

[[0, 1, 2, 3, 4], [5, 6, 7, 8, 9],
 [10, 11, 12, 13, 14], [15, 16, 17, 18, 19],
 [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],
 [80, 81, 82, 83, 84], [85, 86, 87, 88, 89],
 [90, 91, 92, 93, 94], [95, 96, 97, 98, 99]]

6 Comments

I think the OP is working with numpy arrays, regardless, [e for e in l[step:step + n]] is a bit redundant. All you need is [l[step:step + n] for n in range(0, len(l), size)]
@juanpa.arrivillaga, I try running your line, and I am getting an error
Well, I'm not sure what you are running, but if x is your list, then [x[n:5 + n] for n in range(0, len(x),5)] should work
Think about it, [e for e in some_list] is just some_list and since your some_list is already a slice, it's already a copy.
@juanpa.arrivillaga, I ran [l[step:step + n] for n in range(0, len(l), size)], but the code aboves works, and I have updated my answer
|
0
>>> L = range(100)
>>> step = 5
>>> [L[i:i+step] for i in range(0, len(L), step)]
[[0, 1, 2, 3, 4],
 [5, 6, 7, 8, 9],
 [10, 11, 12, 13, 14],
 [15, 16, 17, 18, 19],
 [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],
 [80, 81, 82, 83, 84],
 [85, 86, 87, 88, 89],
 [90, 91, 92, 93, 94],
 [95, 96, 97, 98, 99]]

Seems a natural way to do it to me.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.