python语言简单、方便,其内部可以快速实现排列组合算法,下面做简单介绍:

1、一个列表数据任意组合:【自带的库itertools.combinations】

#_*_ coding:utf-8 _*_
#__author__='dragon'
import itertools
list1 = [1,2,3]
list2 = []
for i in range(1,len(list1)+1):
iter = itertools.combinations(list1,i)
list2.append(list(iter))

print(list2)

 

结果:[[(1,), (2,), (3,)], [(1, 2), (1, 3), (2, 3)], [(1, 2, 3)]]

 

2、排列的实现:【自带的库itertools.permutations】

#_*_ coding:utf-8 _*_
#__author__='dragon'
import itertools
list1 = [1,2,3,4,5]
list2 = []
for i in range(1,len(list1)+1):
iter = itertools.permutations(list1,i)
list2.append(list(iter))

print(list2)

结果:[[(1,), (2,), (3,)], [(1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2)], [(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]]


————————————————
版权声明:本文为CSDN博主「suibianshen2012」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/suibianshen2012/article/details/80772905