Python | 排列与组合
本文简要总结在 Python
中实现排列与组合的方法。
Update: 2022 / 11 / 21
Python | 排列与组合
- 总览
- 方法
- itertools
- 用法
- 示例
- 不考虑顺序
- 考虑顺序
- numpy
- 示例
- 不考虑顺序
- 考虑顺序
- 参考链接
总览
在做数据分析的时候,可能会碰到类似于高中所学过的取小球问题。这时候可以使用已有的库 ( itertools
, numpy
等 ) 与函数或者自己构造相应的方法来达到目的。
方法
比如,我们要实现 1
,2
,3
,4
的排列组合,利用高中曾学过的知识,我们可以很容易写出来 1,如下表:
考虑顺序与否 | 元素个数 | 组合 |
---|---|---|
F
|
1 |
(1,), (2,), (3,), (4,)
|
2 |
(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)
|
|
3 |
(1, 2, 3), (1, 2, 4), (1, 3, 4), (2, 3, 4)
|
|
T
|
1 |
(1,), (2,), (3,), (4,)
|
2 |
(1, 2), (1, 3), (1, 4), (2, 1), (2, 3), (2, 4), (3, 1), (3, 2), (3, 4), (4, 1), (4, 2), (4, 3)
|
|
3 |
(1, 2, 3), (1, 2, 4), (1, 3, 2), (1, 3, 4), (1, 4, 2), (1, 4, 3), (2, 1, 3), (2, 1, 4), (2, 3, 1), (2, 3, 4), (2, 4, 1), (2, 4, 3), (3, 1, 2), (3, 1, 4), (3, 2, 1), (3, 2, 4), (3, 4, 1), (3, 4, 2), (4, 1, 2), (4, 1, 3), (4, 2, 1), (4, 2, 3), (4, 3, 1), (4, 3, 2)
|
itertools
itertools
模块下提供了一些用于生成排列组合的工具函数 2。
用法
方法 | 含义 |
---|---|
product(p, q, … [repeat=1])
|
用序列 p 、q 、…序列中的元素进行排列(元素会重复)。就相当于使用嵌套循环组合。
|
permutations(p[, r])
|
从序列 p 中取出 r 个元素的组成全排列,组合得到元组作为新迭代器的元素。
|
combinations(p, r)
|
从序列 p 中取出 r 个元素组成全组合,元素不允许重复,组合得到元组作为新迭代器的元素。
|
combinations_with_replacement(p, r)
|
从序列 p 中取出 r 个元素组成全组合,元素允许重复,组合得到元组作为新迭代器的元素。
|
示例
以下面的列表为例,
import itertoolsL = [1, 2, 3, 4]
不考虑顺序
comb1 = list(itertools.combinations([1,2,3,4],1))
'''
[(1,), (2,), (3,), (4,)]
'''comb2 = list(itertools.combinations([1,2,3,4],2))
'''
[(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]
'''comb3 = list(itertools.combinations([1,2,3,4],3))
'''
[(1, 2, 3), (1, 2, 4), (1, 3, 4), (2, 3, 4)]
'''
考虑顺序
comb1 = list(itertools.permutations([1,2,3,4],1))
'''
[(1,), (2,), (3,), (4,)]
'''comb2 = list(itertools.permutations([1,2,3,4],2))
'''
[(1, 2), (1, 3), (1, 4), (2, 1), (2, 3), (2, 4), (3, 1), (3, 2), (3, 4), (4, 1), (4, 2), (4, 3)]
'''comb3 = list(itertools.permutations([1,2,3,4],3))
'''
[(1, 2, 3), (1, 2, 4), (1, 3, 2), (1, 3, 4), (1, 4, 2), (1, 4, 3), (2, 1, 3), (2, 1, 4), (2, 3, 1), (2, 3, 4), (2, 4, 1), (2, 4, 3), (3, 1, 2), (3, 1, 4), (3, 2, 1), (3, 2, 4), (3, 4, 1), (3, 4, 2), (4, 1, 2), (4, 1, 3), (4, 2, 1), (4, 2, 3), (4, 3, 1), (4, 3, 2)]
'''
numpy
示例
以下面的列表为例,
import numpy as np
import randomL = [1, 2, 3, 4]
不考虑顺序
elecnt = len(L)
cnt = 2
A = np.product(np.arange(1, elecnt+1))
B = np.product(np.arange(1, elecnt+1-cnt)) * np.product(np.arange(1, 1+cnt))
combcnt = int(A/B)def combinations(combcnt, elecnt, cnt):trials = combcntrslt = []while trials > 0:sample = sorted(random.sample(list(range(1, 1+elecnt)), cnt))if sample in rslt:continueelse:if len(rslt) == combcnt:breakelse:rslt.append(sample)trials -= 1return sorted(rslt)
#
comb = combinations(combcnt=combcnt, elecnt=elecnt, cnt=cnt)
print(f"self-defined: #{len(comb)}: {comb}")
'''
self-defined: #6: [[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]]
'''import itertools
comb = list(itertools.combinations(L, 2))
print(f'itertools : #{len(comb)}: {comb}')
'''
itertools : #6: [(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]
'''
利用上文介绍到的 itertools.combinations
的方法,检验自定义函数 combinations
的结果的准确程度。
考虑顺序
参考这里 3
array_L = np.array(L)
cnt = 3comb = np.unique(np.array(np.meshgrid(array_L, array_L, array_L)).T.reshape(-1, cnt), axis=0)
print(f"\ncomb:\n{comb}")
'''
[[1 1 1][1 2 1][1 3 1][1 4 1][2 1 1][2 2 1]......[4 1 4][4 2 4][4 3 4][4 4 4]]# shape, (64, 3)
'''import itertoolscomb = list(itertools.product(L, repeat=cnt))
'''
[[1 1 1][1 2 1][1 3 1][1 4 1][2 1 1][2 2 1]......[4 1 4][4 2 4][4 3 4][4 4 4]]# shape, (64, 3)
'''
利用上文介绍到的 itertools.product
的方法,检验使用 numpy
所得结果的准确程度。
参考链接
用python实现排列组合 ↩︎
Python的排列组合函数 ↩︎
How to build an array of all combinations of two NumPy arrays? ↩︎
Python | 排列与组合相关推荐
- python 排列组合之itertools
python 排列组合之itertools python 2.6 引入了itertools模块,使得排列组合的实现非常简单:
- python中如何求列表中的和_python实现求解列表中元素的排列和组合
求解列表中元素的排列和组合问题这个问题之前就遇到过几次没有太留意,最近在做题的时候遇上挺多的排列组合问题的,想来有必要温习一下了,今天花点时间写一下,之前都是手工写的,后来知道可以直接使用python ...
- python排列组合
1.Python的排列函数permutations() itertools.permutations(iterable,r=None) 功能:连续返回由iterable序列中的元素生成的长度为r的排列 ...
- python 排列组合算法_python算法-排列组合
原博文 2018-05-07 18:27 − 排列组合 一.递归 1.自己调用自己 2.找到一个退出的条件 二.全排列:针对给定的一组数据,给出包含所有数据的排列的组合 1:1 1,2:[[1,2], ...
- Python排列组合的计算方法
Python排列组合的计算 1. math.comb() 2. scipy 计算排列组合的具体数值 3. itertools 获取排列组合的全部情况数 1. math.comb() 数学模块Pytho ...
- python排列组合函数
之前写过<python组合求和>,使用python排列组合itertools模块combinations函数,本文对该模块的排列组合功能进行整理 官方文档:<itertools - ...
- 数学基础知识-排列与组合
文章目录 前言 一.分步乘法原理 1.定义 2.举例 二.排列 1.定义 2.计算公式 3.举例分析 4.公式推导过程 三.组合 1.定义 2.计算公式 3.举例分析 4.其他一些规定和转换 总结 前 ...
- Permutation test(排列(组合)检验)
2019独角兽企业重金招聘Python工程师标准>>> 对Permutation test 的首次描述可追溯到上个世纪30年代, Fisher( 1935) 和Pitman( 193 ...
- 多重集合的排列和组合问题
多重集合的排列和组合问题 标签: permutationn2c扩展 2012-04-17 16:18 5671人阅读 评论(0) 收藏 举报 分类: 算法(12) 版权声明:本文为博主原创文章,未 ...
最新文章
- JDK1.8 中的双冒号::是什么语法?
- 数据统计脚本(一周)
- 机器学习项目失败的9个原因
- 绝对布局优势_遇上狭长型卫生间基本没救?2种布局教会你,什么叫美观实用兼具...
- C语言 将文件内容转换成Dump文件数据格式
- nested exception is java.lang.ClassNotFoundException: Interceptor
- Duilib嵌入CEF以及JavaScript与C++交互
- php栏目页内容排序问题,Dedecms频道列表页栏目排序问题详解
- 【转载】斐讯K1、K2、K2P 大部分官方固件刷机、刷入Breed 辅助工具教程工具
- 小程序云函数实现微信支付如此简单
- 解决Python无法找到入口——无法定位到程序输入点
- python爬取拉钩网招聘信息分析
- ECharts数据可视化项目
- 看了就会的浏览器帧原理
- MVC实现类似QQ的网页聊天功能-ajax(下)
- [论文笔记]EMNLP2019: Fine-Grained Entity Typing via Hierarchical Multi Graph Convolutional Networks
- maven远程仓库和镜像
- 国际标准码 计算机,蒙古文国际标准编码到形码转换方法、装置及计算机终端与流程...
- 搜索大数据,不应被忽视的金矿
- 企业文控管理系统与ERP/CRM/OA等管理信息系统的集成