我正在使用Python 2.7.
我有一个列表,我想要所有可能的有序组合.
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))
Run Code Online (Sandbox Code Playgroud)
这将给出以下输出:
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 …Run Code Online (Sandbox Code Playgroud)