I was searching for some good pythonic way to do this. I came across this answer that i liked. It is based on deque, like most other answers i found. My Question is if the stuff you want to window over is already contained in an iterable, do we need to do deque
? What are the benfits of that over something much simpler (back to back for
) like this :
s = 'abcd'
start = 0
end = len(s)
for i in range(end):
print(s[start:i+1])
for i in range(1,end):
print('{: >4s}'.format(s[i:end + 1]))
Fancy printed Output :
a
ab
abc
abcd
bcd
cd
d