1

I want to make a list like this:

seq = [1,2,3]
\# implementation
print(result) \# [[1], [1,2], [1,2,3], [2], [2,3], [3]]
laser
  • 1,388
  • 13
  • 14
Daisuke SHIBATO
  • 983
  • 2
  • 11
  • 23
  • Have a look at this. http://stackoverflow.com/questions/1617699/how-to-obtain-all-subsequence-combinations-of-a-string-in-java-or-c-etc The question is for Java, but the first reply also shows the algorithm in python. What you want is the powerset. – molig Nov 29 '16 at 07:08
  • So, do I have to make an original powerset function to eliminate a discontinuous element like [1,3] from a powerset? – Daisuke SHIBATO Nov 29 '16 at 07:16

2 Answers2

0

You can use itertools combination : https://docs.python.org/3/library/itertools.html#itertools.combinations

You can have a look at this answer for more details

Community
  • 1
  • 1
syedelec
  • 1,285
  • 2
  • 19
  • 30
0

Finally I could find a solution by myself:

N = 3
print([list(range(i, j)) for i in range(1,N+2) for j in range(i+1,N+2)])

or,

N = 3
lis = [list(range(i + 1, j + 1)) for (i, j) in itertools.combinations(list(range(N + 1)), 2)]
print(lis)

Thank you!

Daisuke SHIBATO
  • 983
  • 2
  • 11
  • 23