Pythono 实现 Permutation
不管在R 还是python中,都有现成的函数来轻而易举地进行全排列(Permutation)、无序排列等等。今天想要尝试一下使用自己写代码来实现全排列。
首先,我采用的算法如下:
对于一个数列 i.e. 1,2,3,4 想要进行全排列:
在第一个位置可以放入1 ,2,3,4
如果第一个位置为1, 第二个位置则 只能放 2,3,4 ...
如果第一、二个位置为1,2, 第三个位置只能放3 or 4
大致思路:
第一次:[[1],[2],[3],[4]]
第二次:[[[1],[2]],[[1],[3]],[[1],[4]],...]
第三次:[[[1],[2],[3]],[[1],[2],[4]],[[1],[3],[2]],...]
第四次:[[[1],[2],[3],[4]],[[1],[2],[4],[3]],...]
在这样的思想下,写出如下代码:
n = 5
List = [[1],[2],[3],[4],[5]] #数据结构非常重要,如果是[1,2,3,4,5]则很难work!
Grow_List = List
Count = 1
while Count <= (n-1):
Output_List = [] #每一次循环完毕将Output_List归零,这个非常重要。否则会导致Output_List仍包含之前内容
for i in Grow_List:
Temp = List[:] #浅拷贝非常重要!如果不是浅拷贝,将导致List的改变
print "i:",i
if len(i) == 1: #发现在i为一个元素和多个元素的时候,需要做的事情不同,
Temp.remove(i) #将已有元素移除掉,便于进行排列组合
elif len(i) >=2:
for j in i:
Temp.remove(j)
for k in Temp:
if len(i) == 1: #i为一个元素和多个元素的时候,所需操作略有不同
t = [i[:]]
elif len(i) >=2:
t = i[:]
t.append(k) #先对元素进行添加,再添加到Output_List当中
print "t:",t
Output_List.append(t)
Grow_List = Output_List
Count += 1
总之,短短二十多行代码,写了不少时间。看来在算法方面还是需要大大地提高!
Pythono 实现 Permutation的更多相关文章
- Permutation Sequence
The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [LeetCode] Palindrome Permutation II 回文全排列之二
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
- [LeetCode] Palindrome Permutation 回文全排列
Given a string, determine if a permutation of the string could form a palindrome. For example," ...
- [LeetCode] Permutation Sequence 序列排序
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [LeetCode] Next Permutation 下一个排列
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- Leetcode 60. Permutation Sequence
The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- UVA11525 Permutation[康托展开 树状数组求第k小值]
UVA - 11525 Permutation 题意:输出1~n的所有排列,字典序大小第∑k1Si∗(K−i)!个 学了好多知识 1.康托展开 X=a[n]*(n-1)!+a[n-1]*(n-2)!+ ...
- Permutation test: p, CI, CI of P 置换检验相关统计量的计算
For research purpose, I've read a lot materials on permutation test issue. Here is a summary. Should ...
- Permutation
(M) Permutations (M) Permutations II (M) Permutation Sequence (M) Palindrome Permutation II
随机推荐
- 二分图 最大权匹配 km算法
这个算法的本质还是不断的找增广路: KM算法的正确性基于以下定理:若由二分图中所有满足A[i]+B[j]=w[i,j]的边(i,j)构成的子图(称做相等子图)有完备匹配,那么这个完备匹配就是二分图的最 ...
- java 面向对象编程 第20章 XML技术解析
1. XML:extended Markup Language 可扩展标记语言,利用标签和子标签方式描述数据. 2. 声明<?xml version=”1.0”?>版本号 注释< ...
- C#中三种定时器对象的比较
·关于C#中timer类 在C#里关于定时器类就有3个1.定义在System.Windows.Forms里2.定义在System.Threading.Timer类里3.定义在System.Timers ...
- 北邮新生排位赛1解题报告d-e
话说cdsn要是前面插入源代码又什么都不放就会出现奇怪的源代码?不知道是哪个网页的 407. BLOCKS 时间限制 1000 ms 内存限制 65536 KB 题目描述 给定一个N∗M的矩阵,求问里 ...
- 一模 (5) day1
第一题: 题目大意:求出1-10^n 这些数中,包含数字3的有多少个. n<=1000: 解题过程: 1.这题一看就是高精度+递推..如果n=1000,那么假设个位是3,其他999位任意..那么 ...
- 创建一个ROS msg
1. msg •msg:msg文件是简单的文本文件,用于描述ROS中消息(消息的各个参数项).用于为不同的编程语言生成有关消息的源代码. •srv:描述服务的文件,由两部分组成:请求和反馈: msg文 ...
- CodeIgniterCodeigniter+PHPExcel导出数据到Excel文件
解压压缩包里的Classes文件夹中的内容到application\libraries\目录下,目录结构如下:--application\libraries\PHPExcel.php--applica ...
- 倍增 LCA
以NOIP2013提高组day1 最后一道题为例来学的倍增和lca.其实这套题早就做过了,倍增和lca也学过,只不过当时没有理解清楚,所以今天再次学了一遍,虽然没有时间编程序了,但是先把思路和做法在这 ...
- <转>2015-7-14面试题
由于一些原因,最近打算换一份工作,主要目标是大型的互联网公司.在经历了上周三天小公司试水后,昨天终于开始正式的面试之旅了(其实接到面试通知的就几家公司
- Java面向对象的三大特征
Java面向对象的三大特征 java面向对象的三大特征:“封装.继承.多态”.更多Java技术知识,请登陆疯狂软件教育官网.微信搜索微信号:疯狂软件,参加2015年优惠活动,有机会获得优惠劵和代金劵. ...