简体   繁体   English

如何生成范围内所有可能排列的列表

[英]How can I generate a list of all possible permutations in range

I would like to generate words list in a specific way. 我想以特定方式生成单词列表。 I would like to find all permutations for my variable l='EDCMI' not only for 5 characters but for 4 characters, 3 characters etc. 我想查找变量l ='EDCMI'的所有排列,不仅包括5个字符,还包括4个字符,3个字符等。

Just use a nested for loop and remember to add 1 to your range argument to include permutations the same length as your input string: 只需使用一个嵌套的 for循环,并记住在您的范围参数中添加1即可包括与输入字符串长度相同的排列:

from itertools import permutations

s = 'EDCMI'

for i in range(len(s) + 1):
    for p in permutations(s, i):
        print(''.join(p))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM