【问题标题】:itertools: Python Module help creating combinations/permutationsitertools:Python 模块帮助创建组合/排列
【发布时间】:2015-10-22 16:45:22
【问题描述】:

我希望创建不同的单词排列而不改变字符串的单个字符的顺序,而只是改变顺序。

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

python 生成组合

python combinations generate
generate python combinations
generate combinations python
combinations python generate
combinations generate python

我查看了 itertools 模块,但找不到可以打印正确组合的合适迭代器

【问题讨论】:

  • itertools.combinations 会起作用,您只需先将单个字符串拆分为单词(然后重新组合组合元组,也许)。

标签: python


【解决方案1】:
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']

【讨论】:

    猜你喜欢
    • 2012-10-27
    • 2016-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-08
    相关资源
    最近更新 更多