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?
3 Answers
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]])
2 Comments
maelstromscientist
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?
juanpa.arrivillaga
@maelstromscientist using slicing:
arr[::3]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
juanpa.arrivillaga
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)]lmiguelvargasf
@juanpa.arrivillaga, I try running your line, and I am getting an error
juanpa.arrivillaga
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 workjuanpa.arrivillaga
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.lmiguelvargasf
@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 |
>>> 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.
a.reshape(-1,5)ornp.reshape(a, (-1, 5))listdoesn't have a shape.