I am using Python 2.7.
I have a list, and I want all possible ordered combinations.
import itertools
stuff = ["a","b","c", "d"]
for L in range(1, len(stuff)+1):
for subset in itertools.combinations(stuff, L):
print( ' '.join(subset))
This will produce the following result:
a
b
c
d
a b
a c <-- not in correct order
a d <-- not in correct order
b c
b d <-- not in correct order
c d
a b c
a b d <-- not in correct order
a c d <-- not in correct order
b c d
a b c d
But I want only the output to be combinations that are in the same order as the list stuff. For instance. removal of a d, b d, a b dand a c d, because they are not in the correct order as compared to the stufflist ["a", "b", "c", "d"].
I realized using this instead:
import itertools
stuff = ["a","b","c", "d"]
for L in range(1, len(stuff)+1):
for subset in itertools.combinations(stuff, L):
if ' '.join(subset) in ' '.join(stuff):
print( ' '.join(subset))
Gives me the result I wanted:
a
b
c
d
a b
b c
c d
a b c
b c d
a b c d
But is there a Python built-in method that does what I want?