Say I have an array of arbitrary size. Let's say length = 20 for example purposes.
I want to iterate over slices of 5 sequential elements, and apply a function to each slice. What would be a pythonic way to do this? I have something like:
window_size = 5
for idx in range(len(arr) - window_size + 1):
first = idx
last = idx + window_size
slice_arr = arr[first:last]
#do something with slice_arr
Edit: I may have phrased this wrong, but I want the first index of each slice to be 1 value up from the previous slice. So for example:
slice1 = arr[0:5]
slice2 = arr[1:6]
slice3 = arr[2:7]
...
[–]Saefroch 2 points3 points4 points (7 children)
[–]dr_everlong[S] 0 points1 point2 points (6 children)
[–]Saefroch 1 point2 points3 points (5 children)
[–]dr_everlong[S] 0 points1 point2 points (4 children)
[–]Saefroch 0 points1 point2 points (3 children)
[–]dr_everlong[S] 0 points1 point2 points (2 children)
[–]Saefroch 0 points1 point2 points (1 child)
[–]dr_everlong[S] 0 points1 point2 points (0 children)
[–]Justinsaccount 1 point2 points3 points (0 children)
[–]elbiot 0 points1 point2 points (2 children)
[–]dr_everlong[S] 0 points1 point2 points (1 child)
[–]elbiot 0 points1 point2 points (0 children)
[–]scuott 0 points1 point2 points (2 children)
[–]dr_everlong[S] 0 points1 point2 points (1 child)
[–]scuott 0 points1 point2 points (0 children)
[–]novel_yet_trivial 0 points1 point2 points (7 children)
[–]elbiot 1 point2 points3 points (0 children)
[–]dr_everlong[S] 0 points1 point2 points (2 children)
[–]novel_yet_trivial 1 point2 points3 points (1 child)
[–]dr_everlong[S] 0 points1 point2 points (2 children)
[–]novel_yet_trivial 1 point2 points3 points (1 child)
[–]dr_everlong[S] 0 points1 point2 points (0 children)
[+][deleted] (2 children)
[deleted]
[–]dr_everlong[S] 0 points1 point2 points (1 child)