I have an array [1, 2, 3] of integer and I need to return all the possible combination of contiguous sub-arrays of this array.
[1, 2, 3]
[[1],[2],[3],[1,2],
An itertools based approach:
itertools
import itertools def allSubArrays(xs): n = len(xs) indices = list(range(n+1)) for i,j in itertools.combinations(indices,2): yield xs[i:j]
For example:
>>> list(allSubArrays([1,2,3])) [[1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]