Python实现的简单排列组合算法示例


Posted in Python onJuly 04, 2018

本文实例讲述了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,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,), (4,), (5,)], [(1, 2), (1, 3), (1, 4), (1, 5), (2, 1), (2, 3), (2, 4), (2, 5), (3, 1), (3, 2), (3, 4), (3, 5), (4, 1), (4, 2), (4, 3), (4, 5), (5, 1), (5, 2), (5, 3), (5, 4)], [(1, 2, 3), (1, 2, 4), (1, 2, 5), (1, 3, 2), (1, 3, 4), (1, 3, 5), (1, 4, 2), (1, 4, 3), (1, 4, 5), (1, 5, 2), (1, 5, 3), (1, 5, 4), (2, 1, 3), (2, 1, 4), (2, 1, 5), (2, 3, 1), (2, 3, 4), (2, 3, 5), (2, 4, 1), (2, 4, 3), (2, 4, 5), (2, 5, 1), (2, 5, 3), (2, 5, 4), (3, 1, 2), (3, 1, 4), (3, 1, 5), (3, 2, 1), (3, 2, 4), (3, 2, 5), (3, 4, 1), (3, 4, 2), (3, 4, 5), (3, 5, 1), (3, 5, 2), (3, 5, 4), (4, 1, 2), (4, 1, 3), (4, 1, 5), (4, 2, 1), (4, 2, 3), (4, 2, 5), (4, 3, 1), (4, 3, 2), (4, 3, 5), (4, 5, 1), (4, 5, 2), (4, 5, 3), (5, 1, 2), (5, 1, 3), (5, 1, 4), (5, 2, 1), (5, 2, 3), (5, 2, 4), (5, 3, 1), (5, 3, 2), (5, 3, 4), (5, 4, 1), (5, 4, 2), (5, 4, 3)], [(1, 2, 3, 4), (1, 2, 3, 5), (1, 2, 4, 3), (1, 2, 4, 5), (1, 2, 5, 3), (1, 2, 5, 4), (1, 3, 2, 4), (1, 3, 2, 5), (1, 3, 4, 2), (1, 3, 4, 5), (1, 3, 5, 2), (1, 3, 5, 4), (1, 4, 2, 3), (1, 4, 2, 5), (1, 4, 3, 2), (1, 4, 3, 5), (1, 4, 5, 2), (1, 4, 5, 3), (1, 5, 2, 3), (1, 5, 2, 4), (1, 5, 3, 2), (1, 5, 3, 4), (1, 5, 4, 2), (1, 5, 4, 3), (2, 1, 3, 4), (2, 1, 3, 5), (2, 1, 4, 3), (2, 1, 4, 5), (2, 1, 5, 3), (2, 1, 5, 4), (2, 3, 1, 4), (2, 3, 1, 5), (2, 3, 4, 1), (2, 3, 4, 5), (2, 3, 5, 1), (2, 3, 5, 4), (2, 4, 1, 3), (2, 4, 1, 5), (2, 4, 3, 1), (2, 4, 3, 5), (2, 4, 5, 1), (2, 4, 5, 3), (2, 5, 1, 3), (2, 5, 1, 4), (2, 5, 3, 1), (2, 5, 3, 4), (2, 5, 4, 1), (2, 5, 4, 3), (3, 1, 2, 4), (3, 1, 2, 5), (3, 1, 4, 2), (3, 1, 4, 5), (3, 1, 5, 2), (3, 1, 5, 4), (3, 2, 1, 4), (3, 2, 1, 5), (3, 2, 4, 1), (3, 2, 4, 5), (3, 2, 5, 1), (3, 2, 5, 4), (3, 4, 1, 2), (3, 4, 1, 5), (3, 4, 2, 1), (3, 4, 2, 5), (3, 4, 5, 1), (3, 4, 5, 2), (3, 5, 1, 2), (3, 5, 1, 4), (3, 5, 2, 1), (3, 5, 2, 4), (3, 5, 4, 1), (3, 5, 4, 2), (4, 1, 2, 3), (4, 1, 2, 5), (4, 1, 3, 2), (4, 1, 3, 5), (4, 1, 5, 2), (4, 1, 5, 3), (4, 2, 1, 3), (4, 2, 1, 5), (4, 2, 3, 1), (4, 2, 3, 5), (4, 2, 5, 1), (4, 2, 5, 3), (4, 3, 1, 2), (4, 3, 1, 5), (4, 3, 2, 1), (4, 3, 2, 5), (4, 3, 5, 1), (4, 3, 5, 2), (4, 5, 1, 2), (4, 5, 1, 3), (4, 5, 2, 1), (4, 5, 2, 3), (4, 5, 3, 1), (4, 5, 3, 2), (5, 1, 2, 3), (5, 1, 2, 4), (5, 1, 3, 2), (5, 1, 3, 4), (5, 1, 4, 2), (5, 1, 4, 3), (5, 2, 1, 3), (5, 2, 1, 4), (5, 2, 3, 1), (5, 2, 3, 4), (5, 2, 4, 1), (5, 2, 4, 3), (5, 3, 1, 2), (5, 3, 1, 4), (5, 3, 2, 1), (5, 3, 2, 4), (5, 3, 4, 1), (5, 3, 4, 2), (5, 4, 1, 2), (5, 4, 1, 3), (5, 4, 2, 1), (5, 4, 2, 3), (5, 4, 3, 1), (5, 4, 3, 2)], [(1, 2, 3, 4, 5), (1, 2, 3, 5, 4), (1, 2, 4, 3, 5), (1, 2, 4, 5, 3), (1, 2, 5, 3, 4), (1, 2, 5, 4, 3), (1, 3, 2, 4, 5), (1, 3, 2, 5, 4), (1, 3, 4, 2, 5), (1, 3, 4, 5, 2), (1, 3, 5, 2, 4), (1, 3, 5, 4, 2), (1, 4, 2, 3, 5), (1, 4, 2, 5, 3), (1, 4, 3, 2, 5), (1, 4, 3, 5, 2), (1, 4, 5, 2, 3), (1, 4, 5, 3, 2), (1, 5, 2, 3, 4), (1, 5, 2, 4, 3), (1, 5, 3, 2, 4), (1, 5, 3, 4, 2), (1, 5, 4, 2, 3), (1, 5, 4, 3, 2), (2, 1, 3, 4, 5), (2, 1, 3, 5, 4), (2, 1, 4, 3, 5), (2, 1, 4, 5, 3), (2, 1, 5, 3, 4), (2, 1, 5, 4, 3), (2, 3, 1, 4, 5), (2, 3, 1, 5, 4), (2, 3, 4, 1, 5), (2, 3, 4, 5, 1), (2, 3, 5, 1, 4), (2, 3, 5, 4, 1), (2, 4, 1, 3, 5), (2, 4, 1, 5, 3), (2, 4, 3, 1, 5), (2, 4, 3, 5, 1), (2, 4, 5, 1, 3), (2, 4, 5, 3, 1), (2, 5, 1, 3, 4), (2, 5, 1, 4, 3), (2, 5, 3, 1, 4), (2, 5, 3, 4, 1), (2, 5, 4, 1, 3), (2, 5, 4, 3, 1), (3, 1, 2, 4, 5), (3, 1, 2, 5, 4), (3, 1, 4, 2, 5), (3, 1, 4, 5, 2), (3, 1, 5, 2, 4), (3, 1, 5, 4, 2), (3, 2, 1, 4, 5), (3, 2, 1, 5, 4), (3, 2, 4, 1, 5), (3, 2, 4, 5, 1), (3, 2, 5, 1, 4), (3, 2, 5, 4, 1), (3, 4, 1, 2, 5), (3, 4, 1, 5, 2), (3, 4, 2, 1, 5), (3, 4, 2, 5, 1), (3, 4, 5, 1, 2), (3, 4, 5, 2, 1), (3, 5, 1, 2, 4), (3, 5, 1, 4, 2), (3, 5, 2, 1, 4), (3, 5, 2, 4, 1), (3, 5, 4, 1, 2), (3, 5, 4, 2, 1), (4, 1, 2, 3, 5), (4, 1, 2, 5, 3), (4, 1, 3, 2, 5), (4, 1, 3, 5, 2), (4, 1, 5, 2, 3), (4, 1, 5, 3, 2), (4, 2, 1, 3, 5), (4, 2, 1, 5, 3), (4, 2, 3, 1, 5), (4, 2, 3, 5, 1), (4, 2, 5, 1, 3), (4, 2, 5, 3, 1), (4, 3, 1, 2, 5), (4, 3, 1, 5, 2), (4, 3, 2, 1, 5), (4, 3, 2, 5, 1), (4, 3, 5, 1, 2), (4, 3, 5, 2, 1), (4, 5, 1, 2, 3), (4, 5, 1, 3, 2), (4, 5, 2, 1, 3), (4, 5, 2, 3, 1), (4, 5, 3, 1, 2), (4, 5, 3, 2, 1), (5, 1, 2, 3, 4), (5, 1, 2, 4, 3), (5, 1, 3, 2, 4), (5, 1, 3, 4, 2), (5, 1, 4, 2, 3), (5, 1, 4, 3, 2), (5, 2, 1, 3, 4), (5, 2, 1, 4, 3), (5, 2, 3, 1, 4), (5, 2, 3, 4, 1), (5, 2, 4, 1, 3), (5, 2, 4, 3, 1), (5, 3, 1, 2, 4), (5, 3, 1, 4, 2), (5, 3, 2, 1, 4), (5, 3, 2, 4, 1), (5, 3, 4, 1, 2), (5, 3, 4, 2, 1), (5, 4, 1, 2, 3), (5, 4, 1, 3, 2), (5, 4, 2, 1, 3), (5, 4, 2, 3, 1), (5, 4, 3, 1, 2), (5, 4, 3, 2, 1)]]

可以根据你需要随意组合

希望本文所述对大家Python程序设计有所帮助。

Python 相关文章推荐
使用python在校内发人人网状态(人人网看状态)
Feb 19 Python
Python时间的精准正则匹配方法分析
Aug 17 Python
Python爬虫信息输入及页面的切换方法
May 11 Python
解决Matplotlib图表不能在Pycharm中显示的问题
May 24 Python
python实现决策树ID3算法的示例代码
May 30 Python
Python3实现的简单工资管理系统示例
Mar 12 Python
python基于SMTP协议发送邮件
May 31 Python
python实现复制文件到指定目录
Oct 16 Python
Python文件操作函数用法实例详解
Dec 24 Python
python为什么会环境变量设置不成功
Jun 23 Python
python 发送邮件的四种方法汇总
Dec 02 Python
python+selenium小米商城红米K40手机自动抢购的示例代码
Mar 24 Python
把csv文件转化为数组及数组的切片方法
Jul 04 #Python
解决python中使用plot画图,图不显示的问题
Jul 04 #Python
Django框架实现逆向解析url的方法
Jul 04 #Python
pandas 实现字典转换成DataFrame的方法
Jul 04 #Python
详解Django中六个常用的自定义装饰器
Jul 04 #Python
浅析Python pandas模块输出每行中间省略号问题
Jul 03 #Python
python中datetime模块中strftime/strptime函数的使用
Jul 03 #Python
You might like
Erlang的运算符(比较运算符,数值运算符,移位运算符,逻辑运算符)
2012/07/23 PHP
php下Memcached入门实例解析
2015/01/05 PHP
老生常谈PHP面向对象之解释器模式
2017/05/17 PHP
PHP扩展Swoole实现实时异步任务队列示例
2019/04/13 PHP
laravel 实现根据字段不同值做不同查询
2019/10/23 PHP
小试JQuery的AutoComplete插件
2011/05/04 Javascript
Javascript面向对象扩展库代码分享
2012/03/27 Javascript
JavaScript创建一个欢迎cookie弹出窗实现代码
2013/03/15 Javascript
js固定DIV高度,超出部分自动添加滚动条的简单方法
2013/07/10 Javascript
Javascript判断文件是否存在(客户端/服务器端)
2014/09/16 Javascript
Bootstrap布局组件应用实例讲解
2016/02/17 Javascript
详解JS异步加载的三种方式
2017/03/07 Javascript
angular.js指令中transclude选项及ng-transclude指令详解
2017/05/24 Javascript
JavaScript实现轮播图效果代码实例
2019/09/28 Javascript
Vue中Table组件行内右键菜单实现方法(基于 vue + AntDesign)
2019/11/21 Javascript
vue使用nprogress实现进度条
2019/12/09 Javascript
微信小程序中网络请求缓存的解决方法
2019/12/29 Javascript
Element Tooltip 文字提示的使用示例
2020/07/26 Javascript
[01:00:30]完美世界DOTA2联赛循环赛 Inki vs Matador BO2第二场 10.31
2020/11/02 DOTA
Python可变参数函数用法实例
2015/07/07 Python
python 类详解及简单实例
2017/03/24 Python
Python正则表达式分组概念与用法详解
2017/06/24 Python
Python 3.x 安装opencv+opencv_contrib的操作方法
2018/04/02 Python
浅谈python3.6的tkinter运行问题
2019/02/22 Python
python实现多线程端口扫描
2019/08/31 Python
PyCharm 专业版安装图文教程
2020/02/20 Python
python 使用多线程创建一个Buffer缓存器的实现思路
2020/07/02 Python
Links of London官方网站:英国标志性的珠宝品牌
2017/04/09 全球购物
美国羊皮公司:Overland
2018/01/15 全球购物
工作交流会欢迎词
2014/01/12 职场文书
日化店促销方案
2014/03/26 职场文书
2014年绿化工作总结
2014/12/09 职场文书
2015年酒店客房部工作总结
2015/04/25 职场文书
寻找成龙观后感
2015/06/12 职场文书
2019年怎样写好导游词?
2019/07/02 职场文书
MySQL表的增删改查(基础)
2021/04/05 MySQL