I'm using Python 2.7.
I'm having 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 give the following output:
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 the output only to be combinations that are in the same order as the stuff list. E.g. removing a d, b d, a b d and a c d since these are not in correct order compared to the stuff list ["a", "b", "c", "d"].
I've figured out 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): #added line
print( ' '.join(subset))
Is giving me the output 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 any built-in method in Python that does what I want?
a dnot in the correct order? What do you mean order? Are you only interested in slices of your original list? And why isa cin correct order whena dis not?