I have a list, from which I would like to iterate over slices of a certain length, overlapping each other by the largest amount possible, for example:
>>> seq = 'ABCDEF' >>> [''.join(x) for x in zip(seq, seq[1:], seq[2:])] ['ABC', 'BCD', 'CDE', 'DEF']
In other words, is there a shorthand for zip(seq, seq[1:], seq[2:])
where you can specify the length of each sub-sequence?
Advertisement
Answer
[seq[i:i+3] for i in range(len(seq)-2)]
is the Python code for something similar.
The far more elegant and recommended version of this is to use the itertools library from Python (seriously, why do they not just include this function in the library?).
In this case, you would instead use something similar to the pairwise
function provided in the documentation.
from itertools import tee def tripletWise(iterable): "s -> (s0,s1,s2), (s1,s2,s3), (s2,s3,s4), ..." a, b, c = tee(iterable, 3) next(b, None) next(c, None) next(c, None) return zip(a, b) [''.join(i) for i in tripletWise('ABCDEF')] > ['ABC', 'BCD', 'CDE', 'DEF']
You can also create a more general function to chunk the list into however many elements you want to select at a time.
def nWise(iterable, n=2): iterableList = tee(iterable, n) for i in range(len(iterableList)): for j in range(i): next(iterableList[i], None) return zip(*iterableList) [''.join(i) for i in nWise('ABCDEF', 4)] > ['ABCD', 'BCDE', 'CDEF']