Python编程之黑板上排列组合,你舍得解开吗


Posted in Python onOctober 30, 2017

考虑这样一个问题,给定一个矩阵(多维数组,numpy.ndarray()),如何shuffle这个矩阵(也就是对其行进行全排列),如何随机地选择其中的k行,这叫组合,实现一种某一维度空间的切片。例如五列中选三列(全部三列的排列数),便从原有的五维空间中降维到三维空间,因为是全部的排列数,故不会漏掉任何一种可能性。

涉及的函数主要有:

np.random.permutation()
itertools.combinations()
itertools.permutations()

# 1. 对0-5之间的数进行一次全排列
>>>np.random.permutation(6)
array([3, 1, 5, 4, 0, 2])

# 2. 创建待排矩阵
>>>A = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])

# 3. shuffle矩阵A
>>>p = np.random.permutation(A.shape[0])
>>>p
array([1, 2, 0])
>>>A[p, :]     
array([[ 5, 6, 7, 8],
  [ 9, 10, 11, 12],
  [ 1, 2, 3, 4]])

C52的实现

>>>from itertools import combinations
>>>combins = [c for c in combinations(range(5), 2)]
>>>len(combins)
10
>>>combins    # 而且是按序排列
[(0, 1), (0, 2), (0, 3), (0, 4), (1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]

A52的实现

>>>from itertools import permutations
>>>pertumations(range(5), 2)
<itertools.permutations object at 0x0233E360>

>>>perms = permutations(range(5), 2)
>>>perms
[(0, 1), (0, 2), (0, 3), (0, 4), (1, 0), (1, 2), (1, 3), (1, 4), (2, 0), (2, 1),
 (2, 3), (2, 4), (3, 0), (3, 1), (3, 2), (3, 4), (4, 0), (4, 1), (4, 2), (4, 3)]
>>>len(perms)
20
# 5. 任取其中的k(k=2)行
>>>c = [c for c in combinations(range(A.shape[0]), 2)]
>>>A[c[0], :]   # 一种排列
array([[1, 2, 3, 4],
  [5, 6, 7, 8]])

下面再介绍一个列表数据任意组合,主要是利用自带的库

#_*_ 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)]]

排列的实现

#_*_ 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实现排列组合公式C(m,n)求值

# -*- coding:utf-8 -*- 
# 用python实现排列组合C(n,m) = n!/m!*(n-m)! 
def get_value(n): 
 if n==1: 
  return n 
 else: 
  return n * get_value(n-1) 
def gen_last_value(n,m): 
  first = get_value(n) 
  print "n:%s  value:%s"%(n, first) 
  second = get_value(m) 
  print "n:%s  value:%s"%(m, second) 
  third = get_value((n-m)) 
  print "n:%s  value:%s"%((n-m), third) 
  return first/(second * third) 
   
if __name__ == "__main__": 
 # C(12,5) 
 rest = gen_last_value(5,3) 
 print "value:", rest

运行结果:

n:5  value:120
n:3  value:6
n:2  value:2
value: 10

总结

以上就是本文关于Python排列组合算法的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站:Python数据结构与算法之列表(链表,linked list)简单实现、Python算法之求n个节点不同二叉树个数等,有什么问题可以随时留言,小编会及时回复大家的。

Python 相关文章推荐
Python中文件I/O高效操作处理的技巧分享
Feb 04 Python
python进程管理工具supervisor的安装与使用教程
Sep 05 Python
对python 矩阵转置transpose的实例讲解
Apr 17 Python
Django csrf 验证问题的实现
Oct 09 Python
python使用插值法画出平滑曲线
Dec 15 Python
Python实现打砖块小游戏代码实例
May 18 Python
Django集成搜索引擎Elasticserach的方法示例
Jun 04 Python
django ajax发送post请求的两种方法
Jan 05 Python
详解Python修复遥感影像条带的两种方式
Feb 23 Python
python小技巧——将变量保存在本地及读取
Nov 13 Python
浅谈Python数学建模之数据导入
Jun 23 Python
Python办公自动化之教你如何用Python将任意文件转为PDF格式
Jun 28 Python
Python数据结构与算法之列表(链表,linked list)简单实现
Oct 30 #Python
Python进程间通信之共享内存详解
Oct 30 #Python
import的本质解析
Oct 30 #Python
python中hashlib模块用法示例
Oct 30 #Python
浅谈Python由__dict__和dir()引发的一些思考
Oct 30 #Python
Android分包MultiDex策略详解
Oct 30 #Python
Python 判断是否为质数或素数的实例
Oct 30 #Python
You might like
分享下页面关键字抓取www.icbase.com站点代码(带asp.net参数的)
2014/01/30 PHP
PHP实现的简单日历类
2014/11/29 PHP
php过滤所有的空白字符(空格、全角空格、换行等)
2015/10/27 PHP
JSON.stringify 语法实例讲解
2012/03/14 Javascript
一个不错的仿携程自定义数据下拉选择select
2014/09/01 Javascript
在JavaScript中处理时间之getHours()方法的使用
2015/06/10 Javascript
有关json_decode乱码及NULL的问题
2015/10/13 Javascript
Angular2学习笔记——详解路由器模型(Router)
2016/12/02 Javascript
jQuery图片拖动组件Dropzone用法示例
2017/01/17 Javascript
vue代码分割的实现(codesplit)
2018/11/13 Javascript
Electron 如何调用本地模块的方法
2019/02/01 Javascript
flexible.js实现移动端rem适配方案
2020/04/07 Javascript
如何基于jQuery实现五角星评分
2020/09/02 jQuery
[02:16]2018年度CS GO最具人气选手-完美盛典
2018/12/16 DOTA
[03:07]完美世界DOTA2联赛PWL DAY10 决赛集锦
2020/11/11 DOTA
Python写入CSV文件的方法
2015/07/08 Python
Python错误: SyntaxError: Non-ASCII character解决办法
2017/06/08 Python
Python数据结构与算法之图的基本实现及迭代器实例详解
2017/12/12 Python
Python微信公众号开发平台
2018/01/25 Python
利用Django模版生成树状结构实例代码
2019/05/19 Python
Django模板Templates使用方法详解
2019/07/19 Python
python 类的继承 实例方法.静态方法.类方法的代码解析
2019/08/23 Python
python使用 __init__初始化操作简单示例
2019/09/26 Python
基于python图书馆管理系统设计实例详解
2020/08/05 Python
python中pyplot基础图标函数整理
2020/11/10 Python
Python基于Webhook实现github自动化部署
2020/11/28 Python
canvas像素画板的实现代码
2018/11/21 HTML / CSS
世界顶级足球门票网站:Live Football Tickets
2017/10/14 全球购物
欧洲品牌瓷器餐具网上商店:Porzellantreff.de
2018/04/04 全球购物
网络公司美工设计工作个人的自我评价
2013/11/03 职场文书
说明书怎么写
2014/05/06 职场文书
2014党员民主评议个人总结
2014/09/10 职场文书
公司向个人借款协议书范本
2014/10/09 职场文书
家长高考寄语
2015/02/27 职场文书
2015年全国“爱牙日”宣传活动总结
2015/03/23 职场文书
2016年校园社会综合治理宣传月活动总结
2016/03/16 职场文书