itertools:Python模块帮助创建组合/排列

itertools:Python模块帮助创建组合/排列,python,Python,我希望在不改变字符串双字符顺序的情况下创建不同的单词排列,而只是改变顺序 例如,我希望字符串“python generate compositions”打印以下排列: python生成组合 python combinations generate generate python combinations generate combinations python combinations python generate combinations generate python In [1]: f

我希望在不改变字符串双字符顺序的情况下创建不同的单词排列,而只是改变顺序

例如,我希望字符串“python generate compositions”打印以下排列:

python生成组合

python combinations generate
generate python combinations
generate combinations python
combinations python generate
combinations generate python
In [1]: from itertools import permutations

In [2]: map(' '.join, permutations("python generate combinations".split()))
Out[2]:
['python generate combinations',
 'python combinations generate',
 'generate python combinations',
 'generate combinations python',
 'combinations python generate',
 'combinations generate python']

我已经查看了itertools模块,但是找不到一个合适的迭代器来打印正确的组合

itertools。组合
可以工作,您只需要先将单个字符串拆分为单词(然后可能重新组合组合元组)。
python combinations generate
generate python combinations
generate combinations python
combinations python generate
combinations generate python
In [1]: from itertools import permutations

In [2]: map(' '.join, permutations("python generate combinations".split()))
Out[2]:
['python generate combinations',
 'python combinations generate',
 'generate python combinations',
 'generate combinations python',
 'combinations python generate',
 'combinations generate python']