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
一种明显的方法是让for
循环迭代r
的permutations
参数的不同值:
看起来像排列可以采用第二个参数来指定长度。 https://docs.python.org/2/library/itertools.html#itertools.permutations
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)