Python 如何利用带条件的置换得到所有组合

Python 如何利用带条件的置换得到所有组合,python,itertools,Python,Itertools,输出 from itertools import permutations permList = permutations('ABC') for perm in list(permList): print (''.join(perm)) 如何包括得到的2个字母的组合也随着上面的输出,基本上我需要定义的最小长度已经开始是2 另一个例子是字符串ABCDE,这里我需要从3开始minum排列 lenlist

输出

from itertools import permutations 
permList = permutations('ABC')
for perm in list(permList): 
       print (''.join(perm)) 
如何包括得到的2个字母的组合也随着上面的输出,基本上我需要定义的最小长度已经开始是2

另一个例子是字符串ABCDE,这里我需要从3开始minum排列
lenlist<3必须避免

一个明显的方法是使用for循环来迭代排列的r参数的不同值:

从itertools导入置换 对于范围为2,4的x: 对于置换“ABC”中的置换,r=x: 打印 请注意,您可以直接从置换返回的对象进行迭代,因为它是一个

类似地,要获得包含三个以上元素的所有排列,您可以使用:

从itertools导入置换 对于范围为3的x,lenABCDE+1: 对于排列“ABCDE”中的排列,r=x: 打印
您将获得至少具有最小值的所有置换。

看起来置换可以使用第二个参数来指定长度

ABC
ACB
BAC
BCA
CAB
CBA
given_list = [1,2,3,4,5]

min_num = 2

for x in range(min_num, len(given_list)):
    perm = permutations(given_list, x)
    all_perms.extend(perm)
from itertools import permutations 
input_str = 'ABC'
min_len = 2
max_len = len(input_str)

perm_list = []

for l in range(2, max_len + 1):
    l_permList = list(permutations(input_str, l))
    perm_list += l_permList

print (perm_list)