Given [1,2,3,4,5,6,7,8,9,10]
, to get a sliding window of 3 items at a time to get:
[(1, 2, 3), (2, 3, 4), (3, 4, 5), (4, 5, 6), (5, 6, 7), (6, 7, 8), (7, 8, 9), (8, 9, 10)]
From https://stackoverflow.com/q/42220614/610569, a sliding window of a sequence can be achieved with:
def per_window(sequence, n=1):
"""
Returns a sliding window.
From https://stackoverflow.com/q/42220614/610569
>>> list(per_window([1,2,3,4], n=2))
[(1, 2), (2, 3), (3, 4)]
>>> list(per_window([1,2,3,4], n=3))
[(1, 2, 3), (2, 3, 4)]
"""
start, stop = 0, n
seq = list(sequence)
while stop <= len(seq):
yield tuple(seq[start:stop])
start += 1
stop += 1
But if there's some constraints that I want to put in the sliding window, and I only want to get the windows where a certain element exist.
Let's say I only want the windows that contains 4, I could something like:
>>> [window for window in per_window(x, 3) if 4 in window]
[((2, 3, 4), (3, 4, 5), (4,5,6)]
But somehow the loop still has to process through the whole list of windows and check through the if conditions.
I could do some skipping by looking for the positions of 4
and limit the input to the per_window
, e.g.
# Input sequence.
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# Window size.
n = 3
# Constraint.
c = 4
# Set the index to 0
i = 0
while i < len(x)-n:
i = x.index(4, i)
# First window where the constraint is met.
left = i - (n-1)
if left > 0:
print (list(per_window(x[left:i], 3)))
right = i + n
if right < len(x):
print (list(per_window(x[i:right], 3)))
i = right
(Note the code above with the ifs don't work =( )
Instead of finding the index outside of the per_window
function, is there another way to add such a constraint in the per_window
function?
After reading @RaymondHettinger's answer:
def skipping_window(sequence, target, n=3):
"""
Return a sliding window with a constraint to check that
target is inside the window.
From https://stackoverflow.com/q/43626525/610569
"""
start, stop = 0, n
seq = list(sequence)
while stop <= len(seq):
subseq = seq[start:stop]
if target in subseq:
yield tuple(seq[start:stop])
start += 1
stop += 1
# Fast forwarding the start.
# Find the next window which contains the target.
try:
# `seq.index(target, start) - (n-1)` would be the next
# window where the constraint is met.
start = max(seq.index(target, start) - (n-1), start)
stop = start + n
except ValueError:
break
[out]:
>>> x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> list(skipping_window(x, 4, 3))
[(2, 3, 4), (3, 4, 5), (4, 5, 6)]
Instead of finding the index outside of the per_window function, is there another way to add such a constraint in the per_window function?
Yes, you can add the condition just before the yield:
def per_window(sequence, target, n=1):
start, stop = 0, n
seq = list(sequence)
while stop <= len(seq):
subseq = seq[start:stop]
if target in subseq:
yield tuple(subseq)
start += 1
stop += 1