如何使用有条件的置换获得所有组合

问题描述 投票:0回答:3
from itertools import permutations 
permList = permutations('ABC')
for perm in list(permList): 
       print (''.join(perm)) 

输出

ABC
ACB
BAC
BCA
CAB
CBA

如何包括获得两个字母的组合以及上面的输出,基本上我需要定义必须开始的最小长度为2

另一个示例字符串ABCDE在这里,我需要从3开始最小排列(len(list)) < 3 has to avoided

python itertools
3个回答
2
投票

一种明显的方法是让for循环迭代rpermutations参数的不同值:


1
投票

看起来像排列可以采用第二个参数来指定长度。 https://docs.python.org/2/library/itertools.html#itertools.permutations


0
投票
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)
© www.soinside.com 2019 - 2024. All rights reserved.