返回顶部
首页 > 资讯 > 后端开发 > Python >Python编程之黑板上排列组合,你舍得解开吗
  • 658
分享到

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

板上排列组合Python 2022-06-04 19:06:34 658人浏览 八月长安

Python 官方文档:入门教程 => 点击学习

摘要

考虑这样一个问题,给定一个矩阵(多维数组,numpy.ndarray()),如何shuffle这个矩阵(也就是对其行进行全排列),如何随机地选择其中的k行,这叫组合,实现一种某一维度空间的切片。例如五列中选

考虑这样一个问题,给定一个矩阵(多维数组,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个节点不同二叉树个数等,有什么问题可以随时留言,小编会及时回复大家的。

--结束END--

本文标题: Python编程之黑板上排列组合,你舍得解开吗

本文链接: https://lsjlt.com/news/16261.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作