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
随机推荐
- apache本地网址配置
1 实现类似于域名访www.a.com问本地的空间,而不是放在apache下的htocs文件夹下,或者是wamp下的www文件下 2 首先修改C盘WINDOWS\system32\drivers\et ...
- 关于jquery计算页面元素数量
这段jquery计算页面元素数量代码,能不能刷新页面直接输出数量,而不用点计算按钮 <scriptsrc="http://ajax.googleapis.com/ajax/libs/j ...
- POJ 3468 区间更新,区间求和(经典)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 72265 ...
- hdu 4622 Reincarnation
http://acm.hdu.edu.cn/showproblem.php?pid=4622 用字典树把每一个字符串对应成一个整数 相同的字符串对应到相同的整数上 把所用的串对应的整数放在一个数组里 ...
- 矩阵(matrix)
我们定义一个矩阵的权值为这个矩阵四个角上的数值的最小值.现在小M有一个矩阵,他想在这个矩阵中寻找到一个权值最大的子矩阵,请你告诉他这个最大权值.(距形规模最大为2000*2000) 比赛 看到第二题那 ...
- visual studio 2013连接Oracle 11g并获取数据:(一:环境搭建)
C# WinForm案例: 目标: visual studio 中点击按钮,就可获取到Oracle中数据表的内容 1.安装Visual Studio 2013 ,推荐如下网址,下载ISO镜像,一路ne ...
- [开发笔记]-多线程异步操作如何访问HttpContext?
如何获取文件绝对路径? 在定时器回调或者Cache的移除通知中,有时确实需要访问文件,然而对于开发人员来说, 他们并不知道网站会被部署在哪个目录下,因此不可能写出绝对路径, 他们只知道相对于网站根目录 ...
- centos7 学习1 KDE配置中文
安装kde桌面后没有中文,可以用以下方法配置中文 #yum list kde*chinese 会显示可以安装的包,我的显示如下 kde-l10n-Chinese.noarch -.fc14 @upda ...
- JAVA工作方式
public class Hellow { int eyes = 2; int ears = 2; int legs = 4; void run(){ //方法 System.out.println( ...
- 内部类中class声明地方不同,效果不一样
1.一个声明在类中,一个声明在类的方法中.在类中的方法中声明内部类,其方法中的内部类调用 内部类外中的变量,变量必须final class Outter{ int x1 = 0; public voi ...