【问题标题】:How can I generate a list of all possible permutations in range如何生成范围内所有可能排列的列表
【发布时间】:2019-04-24 14:41:23
【问题描述】:

我想以特定方式生成单词列表。我想找到我的变量 l='EDCMI' 的所有排列,不仅适用于 5 个字符,而且适用于 4 个字符、3 个字符等。

【问题讨论】:

    标签: python python-3.x for-loop permutation itertools


    【解决方案1】:

    只需使用 nested for 循环并记住将 1 添加到您的范围参数以包含与输入字符串长度相同的排列:

    from itertools import permutations
    
    s = 'EDCMI'
    
    for i in range(len(s) + 1):
        for p in permutations(s, i):
            print(''.join(p))
    

    【讨论】:

    • 如果您不想要空元素'',则必须将外循环的范围从 1 开始
    猜你喜欢
    • 2012-05-05
    • 2013-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多