python实现求解列表中元素的排列和组合问题


Posted in Python onMarch 15, 2018

 求解列表中元素的排列和组合问题这个问题之前就遇到过几次没有太留意,最近在做题的时候遇上挺多的排列组合问题的,想来有必要温习一下了,今天花点时间写一下,之前都是手工写的,后来知道可以直接使用python的内置模块就可以完成这个工作了,今天就使用python的itertools模块来完成这个工作,一共解决四个问题:

1.生成排列,列表中元素不允许重复出现

2.生成排列,列表中元素可以重复出现

3.生成组合,不限元素个数,列表中元素不允许重复出现

4.生成组合,不限元素个数,列表中元素可以重复出现

    因为大家都有排列组合的知识这里就不累赘了,问题很简单,下面看具体的实现:

#!usr/bin/env python 
#encoding:utf-8 
''''' 
__Author__:沂水寒城 
功能:求解列表中元素的排列和组合问题 
''' 
from itertools import product 
from itertools import combinations 
import itertools 
def test_func1(num_list): 
 ''''' 
 生成排列 
 列表中元素不允许重复出现 
 排列数计算为:n!,其中n为num_list列表中元素个数 
 ''' 
 tmp_list = itertools.permutations(num_list) 
 res_list=[] 
 for one in tmp_list: 
  res_list.append(one) 
 print res_list 
 print '元素不允许重复出现排列总数为:', len(res_list) 
def test_func11(num_list): 
 ''''' 
 生成排列 
 列表中元素可以重复出现 
 排列总数计算为:(n*n*n...*n),一共n个n相乘 
 ''' 
 num=len(num_list) 
 res_list=list(product(num_list,repeat=num)) 
 print res_list 
 print '元素可以重复出现排列总数为:', len(res_list) 
def test_func2(num_list): 
 ''''' 
 生成组合,不限元素个数 
 列表中元素不允许重复出现 
 组合数计算为:2^n,其中n为num_list列表中元素个数 
 ''' 
 res_list=[] 
 for i in range(len(num_list)+1): 
  res_list+=list(combinations(num_list, i)) 
 print res_list 
 print '元素不允许重复出现组合总数为:', len(res_list) 
def test_func22(num_list): 
 ''''' 
 生成组合,不限元素个数 
 列表中元素可以重复出现 
 ''' 
 res_list=[] 
 num_list1=[str(i) for i in num_list] 
 for i in range(0,len(num_list)+1): 
  res_list+=[''.join(x) for x in itertools.product(*[num_list1] * i)] 
 print res_list 
 print '元素可以重复出现组合总数为:', len(res_list) 
if __name__ == '__main__': 
 num_list=[1,2,3,4] 
 test_func1(num_list) 
 print '-------------------------------------' 
 test_func11(num_list) 
 print '-------------------------------------' 
 test_func2(num_list) 
 print '-------------------------------------' 
 test_func22(num_list)

结果如下:

[(1, 2, 3, 4), (1, 2, 4, 3), (1, 3, 2, 4), (1, 3, 4, 2), (1, 4, 2, 3), (1, 4, 3, 2), (2, 1, 3, 4), (2, 1, 4, 3), (2, 3, 1, 4), (2, 3, 4, 1), (2, 4, 1, 3), (2, 4, 3, 1), (3, 1, 2, 4), (3, 1, 4, 2), (3, 2, 1, 4), (3, 2, 4, 1), (3, 4, 1, 2), (3, 4, 2, 1), (4, 1, 2, 3), (4, 1, 3, 2), (4, 2, 1, 3), (4, 2, 3, 1), (4, 3, 1, 2), (4, 3, 2, 1)] 
元素不允许重复出现排列总数为: 24 
------------------------------------- 
[(1, 1, 1, 1), (1, 1, 1, 2), (1, 1, 1, 3), (1, 1, 1, 4), (1, 1, 2, 1), (1, 1, 2, 2), (1, 1, 2, 3), (1, 1, 2, 4), (1, 1, 3, 1), (1, 1, 3, 2), (1, 1, 3, 3), (1, 1, 3, 4), (1, 1, 4, 1), (1, 1, 4, 2), (1, 1, 4, 3), (1, 1, 4, 4), (1, 2, 1, 1), (1, 2, 1, 2), (1, 2, 1, 3), (1, 2, 1, 4), (1, 2, 2, 1), (1, 2, 2, 2), (1, 2, 2, 3), (1, 2, 2, 4), (1, 2, 3, 1), (1, 2, 3, 2), (1, 2, 3, 3), (1, 2, 3, 4), (1, 2, 4, 1), (1, 2, 4, 2), (1, 2, 4, 3), (1, 2, 4, 4), (1, 3, 1, 1), (1, 3, 1, 2), (1, 3, 1, 3), (1, 3, 1, 4), (1, 3, 2, 1), (1, 3, 2, 2), (1, 3, 2, 3), (1, 3, 2, 4), (1, 3, 3, 1), (1, 3, 3, 2), (1, 3, 3, 3), (1, 3, 3, 4), (1, 3, 4, 1), (1, 3, 4, 2), (1, 3, 4, 3), (1, 3, 4, 4), (1, 4, 1, 1), (1, 4, 1, 2), (1, 4, 1, 3), (1, 4, 1, 4), (1, 4, 2, 1), (1, 4, 2, 2), (1, 4, 2, 3), (1, 4, 2, 4), (1, 4, 3, 1), (1, 4, 3, 2), (1, 4, 3, 3), (1, 4, 3, 4), (1, 4, 4, 1), (1, 4, 4, 2), (1, 4, 4, 3), (1, 4, 4, 4), (2, 1, 1, 1), (2, 1, 1, 2), (2, 1, 1, 3), (2, 1, 1, 4), (2, 1, 2, 1), (2, 1, 2, 2), (2, 1, 2, 3), (2, 1, 2, 4), (2, 1, 3, 1), (2, 1, 3, 2), (2, 1, 3, 3), (2, 1, 3, 4), (2, 1, 4, 1), (2, 1, 4, 2), (2, 1, 4, 3), (2, 1, 4, 4), (2, 2, 1, 1), (2, 2, 1, 2), (2, 2, 1, 3), (2, 2, 1, 4), (2, 2, 2, 1), (2, 2, 2, 2), (2, 2, 2, 3), (2, 2, 2, 4), (2, 2, 3, 1), (2, 2, 3, 2), (2, 2, 3, 3), (2, 2, 3, 4), (2, 2, 4, 1), (2, 2, 4, 2), (2, 2, 4, 3), (2, 2, 4, 4), (2, 3, 1, 1), (2, 3, 1, 2), (2, 3, 1, 3), (2, 3, 1, 4), (2, 3, 2, 1), (2, 3, 2, 2), (2, 3, 2, 3), (2, 3, 2, 4), (2, 3, 3, 1), (2, 3, 3, 2), (2, 3, 3, 3), (2, 3, 3, 4), (2, 3, 4, 1), (2, 3, 4, 2), (2, 3, 4, 3), (2, 3, 4, 4), (2, 4, 1, 1), (2, 4, 1, 2), (2, 4, 1, 3), (2, 4, 1, 4), (2, 4, 2, 1), (2, 4, 2, 2), (2, 4, 2, 3), (2, 4, 2, 4), (2, 4, 3, 1), (2, 4, 3, 2), (2, 4, 3, 3), (2, 4, 3, 4), (2, 4, 4, 1), (2, 4, 4, 2), (2, 4, 4, 3), (2, 4, 4, 4), (3, 1, 1, 1), (3, 1, 1, 2), (3, 1, 1, 3), (3, 1, 1, 4), (3, 1, 2, 1), (3, 1, 2, 2), (3, 1, 2, 3), (3, 1, 2, 4), (3, 1, 3, 1), (3, 1, 3, 2), (3, 1, 3, 3), (3, 1, 3, 4), (3, 1, 4, 1), (3, 1, 4, 2), (3, 1, 4, 3), (3, 1, 4, 4), (3, 2, 1, 1), (3, 2, 1, 2), (3, 2, 1, 3), (3, 2, 1, 4), (3, 2, 2, 1), (3, 2, 2, 2), (3, 2, 2, 3), (3, 2, 2, 4), (3, 2, 3, 1), (3, 2, 3, 2), (3, 2, 3, 3), (3, 2, 3, 4), (3, 2, 4, 1), (3, 2, 4, 2), (3, 2, 4, 3), (3, 2, 4, 4), (3, 3, 1, 1), (3, 3, 1, 2), (3, 3, 1, 3), (3, 3, 1, 4), (3, 3, 2, 1), (3, 3, 2, 2), (3, 3, 2, 3), (3, 3, 2, 4), (3, 3, 3, 1), (3, 3, 3, 2), (3, 3, 3, 3), (3, 3, 3, 4), (3, 3, 4, 1), (3, 3, 4, 2), (3, 3, 4, 3), (3, 3, 4, 4), (3, 4, 1, 1), (3, 4, 1, 2), (3, 4, 1, 3), (3, 4, 1, 4), (3, 4, 2, 1), (3, 4, 2, 2), (3, 4, 2, 3), (3, 4, 2, 4), (3, 4, 3, 1), (3, 4, 3, 2), (3, 4, 3, 3), (3, 4, 3, 4), (3, 4, 4, 1), (3, 4, 4, 2), (3, 4, 4, 3), (3, 4, 4, 4), (4, 1, 1, 1), (4, 1, 1, 2), (4, 1, 1, 3), (4, 1, 1, 4), (4, 1, 2, 1), (4, 1, 2, 2), (4, 1, 2, 3), (4, 1, 2, 4), (4, 1, 3, 1), (4, 1, 3, 2), (4, 1, 3, 3), (4, 1, 3, 4), (4, 1, 4, 1), (4, 1, 4, 2), (4, 1, 4, 3), (4, 1, 4, 4), (4, 2, 1, 1), (4, 2, 1, 2), (4, 2, 1, 3), (4, 2, 1, 4), (4, 2, 2, 1), (4, 2, 2, 2), (4, 2, 2, 3), (4, 2, 2, 4), (4, 2, 3, 1), (4, 2, 3, 2), (4, 2, 3, 3), (4, 2, 3, 4), (4, 2, 4, 1), (4, 2, 4, 2), (4, 2, 4, 3), (4, 2, 4, 4), (4, 3, 1, 1), (4, 3, 1, 2), (4, 3, 1, 3), (4, 3, 1, 4), (4, 3, 2, 1), (4, 3, 2, 2), (4, 3, 2, 3), (4, 3, 2, 4), (4, 3, 3, 1), (4, 3, 3, 2), (4, 3, 3, 3), (4, 3, 3, 4), (4, 3, 4, 1), (4, 3, 4, 2), (4, 3, 4, 3), (4, 3, 4, 4), (4, 4, 1, 1), (4, 4, 1, 2), (4, 4, 1, 3), (4, 4, 1, 4), (4, 4, 2, 1), (4, 4, 2, 2), (4, 4, 2, 3), (4, 4, 2, 4), (4, 4, 3, 1), (4, 4, 3, 2), (4, 4, 3, 3), (4, 4, 3, 4), (4, 4, 4, 1), (4, 4, 4, 2), (4, 4, 4, 3), (4, 4, 4, 4)] 
元素可以重复出现排列总数为: 256 
------------------------------------- 
[(), (1,), (2,), (3,), (4,), (1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4), (1, 2, 3), (1, 2, 4), (1, 3, 4), (2, 3, 4), (1, 2, 3, 4)] 
元素不允许重复出现组合总数为: 16 
------------------------------------- 
['', '1', '2', '3', '4', '11', '12', '13', '14', '21', '22', '23', '24', '31', '32', '33', '34', '41', '42', '43', '44', '111', '112', '113', '114', '121', '122', '123', '124', '131', '132', '133', '134', '141', '142', '143', '144', '211', '212', '213', '214', '221', '222', '223', '224', '231', '232', '233', '234', '241', '242', '243', '244', '311', '312', '313', '314', '321', '322', '323', '324', '331', '332', '333', '334', '341', '342', '343', '344', '411', '412', '413', '414', '421', '422', '423', '424', '431', '432', '433', '434', '441', '442', '443', '444', '1111', '1112', '1113', '1114', '1121', '1122', '1123', '1124', '1131', '1132', '1133', '1134', '1141', '1142', '1143', '1144', '1211', '1212', '1213', '1214', '1221', '1222', '1223', '1224', '1231', '1232', '1233', '1234', '1241', '1242', '1243', '1244', '1311', '1312', '1313', '1314', '1321', '1322', '1323', '1324', '1331', '1332', '1333', '1334', '1341', '1342', '1343', '1344', '1411', '1412', '1413', '1414', '1421', '1422', '1423', '1424', '1431', '1432', '1433', '1434', '1441', '1442', '1443', '1444', '2111', '2112', '2113', '2114', '2121', '2122', '2123', '2124', '2131', '2132', '2133', '2134', '2141', '2142', '2143', '2144', '2211', '2212', '2213', '2214', '2221', '2222', '2223', '2224', '2231', '2232', '2233', '2234', '2241', '2242', '2243', '2244', '2311', '2312', '2313', '2314', '2321', '2322', '2323', '2324', '2331', '2332', '2333', '2334', '2341', '2342', '2343', '2344', '2411', '2412', '2413', '2414', '2421', '2422', '2423', '2424', '2431', '2432', '2433', '2434', '2441', '2442', '2443', '2444', '3111', '3112', '3113', '3114', '3121', '3122', '3123', '3124', '3131', '3132', '3133', '3134', '3141', '3142', '3143', '3144', '3211', '3212', '3213', '3214', '3221', '3222', '3223', '3224', '3231', '3232', '3233', '3234', '3241', '3242', '3243', '3244', '3311', '3312', '3313', '3314', '3321', '3322', '3323', '3324', '3331', '3332', '3333', '3334', '3341', '3342', '3343', '3344', '3411', '3412', '3413', '3414', '3421', '3422', '3423', '3424', '3431', '3432', '3433', '3434', '3441', '3442', '3443', '3444', '4111', '4112', '4113', '4114', '4121', '4122', '4123', '4124', '4131', '4132', '4133', '4134', '4141', '4142', '4143', '4144', '4211', '4212', '4213', '4214', '4221', '4222', '4223', '4224', '4231', '4232', '4233', '4234', '4241', '4242', '4243', '4244', '4311', '4312', '4313', '4314', '4321', '4322', '4323', '4324', '4331', '4332', '4333', '4334', '4341', '4342', '4343', '4344', '4411', '4412', '4413', '4414', '4421', '4422', '4423', '4424', '4431', '4432', '4433', '4434', '4441', '4442', '4443', '4444'] 
元素可以重复出现组合总数为: 341 
[Finished in 0.4s]

总结

以上所述是小编给大家介绍的python实现求解列表中元素的排列和组合问题,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!

Python 相关文章推荐
python中正则表达式的使用详解
Oct 17 Python
wxPython定时器wx.Timer简单应用实例
Jun 03 Python
Python操作MySQL数据库的三种方法总结
Jan 30 Python
Python基于win32ui模块创建弹出式菜单示例
May 09 Python
解决Django的request.POST获取不到内容的问题
May 28 Python
在python中安装basemap的教程
Sep 20 Python
python3调用windows dos命令的例子
Aug 14 Python
django实现模板中的字符串文字和自动转义
Mar 31 Python
Mac PyCharm中的.gitignore 安装设置教程
Apr 16 Python
如何基于python实现年会抽奖工具
Oct 20 Python
详解python中[-1]、[:-1]、[::-1]、[n::-1]使用方法
Apr 25 Python
使用Pytorch实现two-head(多输出)模型的操作
May 28 Python
Python遍历某目录下的所有文件夹与文件路径
Mar 15 #Python
Python cookbook(数据结构与算法)找出序列中出现次数最多的元素算法示例
Mar 15 #Python
ubuntu安装sublime3并配置python3环境的方法
Mar 15 #Python
Centos7 Python3下安装scrapy的详细步骤
Mar 15 #Python
python实现word 2007文档转换为pdf文件
Mar 15 #Python
python中使用PIL制作并验证图片验证码
Mar 15 #Python
Python读取Word(.docx)正文信息的方法
Mar 15 #Python
You might like
PHP 输出URL的快捷方式示例代码
2013/09/22 PHP
CentOS 安装 PHP5.5+Redis+XDebug+Nginx+MySQL全纪录
2015/03/25 PHP
Yii2汉字转拼音类的实例代码
2017/04/18 PHP
ajax调用返回php接口返回json数据的方法(必看篇)
2017/05/05 PHP
jquery ajax对特殊字符进行转义防止js注入使用示例
2013/11/21 Javascript
js检测判断日期大于多少天的方法
2015/05/04 Javascript
Bootstrap中表单控件状态(验证状态)
2016/08/04 Javascript
SelecT下拉框选中和取值的解决方法
2016/11/22 Javascript
jquery与ajax获取特殊字符实例详解
2017/01/08 Javascript
JavaScript基于replace+正则实现ES6的字符串模版功能
2017/04/25 Javascript
js实现图片懒加载效果
2017/07/17 Javascript
vue做网页开场视频的实例代码
2017/10/20 Javascript
小程序getLocation需要在app.json中声明permission字段
2019/04/04 Javascript
python元组操作实例解析
2014/09/23 Python
Python3简单实例计算同花的概率代码
2017/12/06 Python
Python logging管理不同级别log打印和存储实例
2018/01/19 Python
Python使用sorted对字典的key或value排序
2018/11/15 Python
Python设计模式之模板方法模式实例详解
2019/01/17 Python
快速解决vue.js 模板和jinja 模板冲突的问题
2019/07/26 Python
Python对列表的操作知识点详解
2019/08/20 Python
Python 类属性与实例属性,类对象与实例对象用法分析
2019/09/20 Python
wxPython实现画图板
2020/08/27 Python
scrapy在python爬虫中搭建出错的解决方法
2020/11/22 Python
HTML5 移动页面自适应手机屏幕四类方法总结
2017/08/17 HTML / CSS
世界首屈一指的钓鱼用品商店:TackleDirect
2016/07/26 全球购物
Bibloo匈牙利:女装、男装、童装及鞋子和配饰
2019/04/14 全球购物
马来西亚在线健康商店:Medipal Malaysia
2020/04/13 全球购物
HashMap和Hashtable的区别
2013/05/18 面试题
"引用"与多态的关系
2013/02/01 面试题
村委会换届选举方案
2014/05/03 职场文书
求职信怎么写范文
2014/05/26 职场文书
实习护士自荐信
2014/06/21 职场文书
财务审计整改报告
2014/11/06 职场文书
青年文明号申报材料
2014/12/23 职场文书
瞿秋白纪念馆观后感
2015/06/10 职场文书
2016大学生暑期社会实践心得体会
2016/01/14 职场文书