Wrapping around a list

amjadcsu at gmail.com amjadcsu at gmail.com
Wed Nov 27 11:46:50 CET 2013

I am working on a problem (Bioinformatics domain) where all possible combinations of input string needs to be printed as sublist

For example:
Input string : "LEQN"
Output= "[L","E","Q","N"]["LE","EQ","QN","NL"] ["LEQ","EQN","QNE","NLE"]

The code i have written for this is as follows:

from itertools import chain, repeat, islice
from collections import deque

def sliding_window(iterable, n, fill=False, fillvalue=None):
    it = iter(iterable)
    if fill:
        it = chain(it, repeat(fillvalue, n - 1))
    w = deque(islice(it, n - 1))
    for x in it:
        yield w


lstinput= list(input)
list1=[''.join(x) for x in sliding_window(lstinput, 2)]
list2= [''.join(x) for x  in sliding_window(lstinput, 3)]
list3= [''.join(x) for x in sliding_window(lstinput, 4)]

The output i get as follows:
List 1 is  ['LE', 'EN', 'NQ']   Should be ['LE','EN','NQ','QL']
 List 2 is  ['LEN', 'ENQ']      Should be ['LEN','ENQ','NQL','QLE']

So the question i am asking , how can i add wrapping around sublist in my sliding window function.


More information about the Python-list mailing list