Generate a list of strings with a sliding window using itertools, yield, and iter() in Python 2.7.1?
You mean you want to do this ? :
a='abcdefg'
b = [a[i:i+3] for i in xrange(len(a)-2)]
print b
['abc', 'bcd', 'cde', 'def', 'efg']
Your generator could be much shorter:
def window(fseq, window_size=5):
for i in xrange(len(fseq) - window_size + 1):
yield fseq[i:i+window_size]
for seq in window('abcdefghij', 3):
print seq
abc
bcd
cde
def
efg
fgh
ghi
hij