0

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
Community
  • 1
  • 1
Somjit
  • 2,503
  • 5
  • 33
  • 60

1 Answers1

0

Indeed, if all your data is already in memory, then simply keeping track of the beginning and end of your window is enough. However, that is not always the case - in signal processing, for example, you are typically receiving a stream of data that you need to process and then either write to disk or stream to some other component. In that situation, you will want to use some form of queue rather than a list because removing elements from the front of a long list is expensive.

Aasmund Eldhuset
  • 37,289
  • 4
  • 68
  • 81