本文实例讲述了Python实现的简单排列组合算法。分享给大家供大家参考,具体如下:
1.python语言简单、方便,其内部可以快速实现排列组合算法,下面做简单介绍
2.一个列表数据任意组合
主要是利用自带的库:
#_*_ coding:utf-8 _*_
#__author__=’dragon’
import itertools
list1 = [1,2,3,4,5]
list2 = []
for i in range(1,len(list1)+1):
iter = itertools.combinations(list1,i)
list2.append(list(iter))
print(list2)
运行结果:
[[(1,), (2,), (3,), (4,), (5,)], [(1, 2), (1, 3), (1, 4), (1, 5), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5), (4, 5)], [(1, 2, 3), (1, 2, 4), (1, 2, 5), (1, 3, 4), (1, 3, 5), (1, 4, 5), (2, 3, 4), (2, 3, 5), (2, 4, 5), (3, 4, 5)], [(1, 2, 3, 4), (1, 2, 3, 5), (1, 2, 4, 5), (1, 3, 4, 5), (2, 3, 4, 5)], [(1, 2, 3, 4, 5)]]
3.排列的实现
#_*_ coding:utf-8 _*_
#__author__=’dragon’
import itertools
list1 = [1