不管在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的更多相关文章

  1. Permutation Sequence

    The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  2. [LeetCode] Palindrome Permutation II 回文全排列之二

    Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...

  3. [LeetCode] Palindrome Permutation 回文全排列

    Given a string, determine if a permutation of the string could form a palindrome. For example," ...

  4. [LeetCode] Permutation Sequence 序列排序

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  5. [LeetCode] Next Permutation 下一个排列

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  6. 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 ...

  7. UVA11525 Permutation[康托展开 树状数组求第k小值]

    UVA - 11525 Permutation 题意:输出1~n的所有排列,字典序大小第∑k1Si∗(K−i)!个 学了好多知识 1.康托展开 X=a[n]*(n-1)!+a[n-1]*(n-2)!+ ...

  8. 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 ...

  9. Permutation

    (M) Permutations (M) Permutations II (M) Permutation Sequence (M) Palindrome Permutation II

随机推荐

  1. 学习记录014-ssh批量分发

    一.ssh服务介绍 1.ssh安全的加密协议用于远程连接服务器 2.默认端口是22,安全协议版本ssh2,它能同时支持RSA和DSA秘钥,SSH1只支持RSA 3.服务端主要包含两个服务功能ssh远程 ...

  2. 用PHP做服务器接口客户端用http协议POST访问安全性一般怎么做

    我的问题是,如果不做安全相关处理的话,一些可能改变数据库的操作可能会遭遇垃圾数据提交什么的,毕竟要找到这些信息只要找个http包就可以了 系统无用户登录 新手问题(从来没做过服务端开发),如果可以,给 ...

  3. 启动项目报错Error: listen EADDRINUSE

    我在使用elasticsearch的kibana插件时候,有一次启动,遇到这个错误: Error: listen EADDRINUSE 它的意思是,端口5601被其他进程占用. 故而,需要kill掉那 ...

  4. DB2配置信息查看及其更新命令

    获取DB2配置信息 db2 get dbm cfg 更新DB2链接配置信息 db2 update dbm cfg using authentication server db2stop db2star ...

  5. English test for certificate

    <英语口译全真试题精解> GPA 3.5 (MTI)  (CPA) ( GRE )  1350分+3.5以上 SAT ( TOEFL )  100分以上 TOEIC BEC (剑桥商务英语 ...

  6. struts2内Action方法调用

    1.struts2流程: jsp页面-->web.xml-->struts.xml-->user.acrion-->UserAction.java 中的execute()--r ...

  7. MySQL校对规则(三)

    校对规则:在当前编码下,字符之间的比较顺序是什么? ci:不区分大小写,Cs区分大小写, _bin 编码比较 每个字符集都支持不定数量的校对规则,可以通过如下指令: show collation 可以 ...

  8. poj1647

    转自:http://woodjohn.blog.sohu.com/231905679.html 题意是比较简单的:假定你是国际象棋中的白方,现在棋盘上只剩下白王.黑王和白后(王和后的走法规则就不赘述了 ...

  9. Ogre 1.8 terrain 和 paging 组件

    以下转自:http://hi.baidu.com/xocoder/item/e8d87cf53d87612b753c4cfd OGRE地形生成 OGRE可以通过两个接口来生成地形,分别是void Te ...

  10. 功率与dbm的对照表

     功率与dbm的对照表 分类: 嵌入式 功率与dbm的对照表 对于无线工程师来说更常用分贝dBm这个单位,dBm单位表示相对于1毫瓦的分贝数,dBm和W之间的关系是:dBm=10*lg(mW)1w的功 ...