相关疑难解决方法(0)

Python:生成列表的所有有序组合

我正在使用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)

python combinations list python-itertools

8
推荐指数
1
解决办法
2573
查看次数

标签 统计

combinations ×1

list ×1

python ×1

python-itertools ×1