不管在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. HDU 1394 树状数组求逆序对

    Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

  2. Jquery easyui datagrid 导出Excel

    From:http://www.cnblogs.com/weiqt/articles/4022399.html datagrid的扩展方法,用于将当前的数据生成excel需要的内容. 1 <sc ...

  3. bzoj 3529 数表 莫比乌斯反演+树状数组

    题目大意: 有一张N×m的数表,其第i行第j列(1 < =i < =礼,1 < =j < =m)的数值为能同时整除i和j的所有自然数之和.给定a,计算数表中不大于a的数之和. ...

  4. LightOJ 1141 Program E

    Description In this problem, you are given an integer number s. You can transform any integer number ...

  5. C++指针(部分有误需修改)

    一.取地址运算符&(内存地址) C++编译的程序占用的内存分为以下几个部分: 1.栈区:由编译器自动分配释放 ,存放函数的参数值,局部变量的值等.其操作方式类似于数据结构中的栈.与其它分区不同 ...

  6. linux之开发板与宿主机-GDB远程调试

    平台: redhat9.0 下载 gdb-5.3.tar.gz 解压 gdb-5.3.tar.gz: #tar vzxf gdb-5.3.tar.gz 配置文件# ./configure - targ ...

  7. matlab图形句柄属性总结

    原文在于雪漫的bloghttp://blog.sina.com.cn/s/blog_4b9b714a0100cce2.html这两天在看句柄式图形方面的东西,以下是我在看书过程中整理的学习笔记,比较详 ...

  8. springMVC文件上传(转)

    原文链接: http://www.cnblogs.com/lonecloud/p/5989905.html 在Spring-mvc.xml注入bean 1 <!-- 配置文件上传,如果没有使用文 ...

  9. iPhone 6/6 Plus国行版开卖当日抢购攻略

    在距离苹果首批发售时隔一个月也就是北京时间10月17日,苹果iPhone 6.iPhone 6 Plus终于也要在中国大陆开卖,众多国内用户终于有机会安排自己的购机计划.据不完全数据显示,目前iPho ...

  10. SharePoint开发 - Excel数据导入到SharePoint自定义列表(数据视图方式)

    博客地址 http://blog.csdn.net/foxdave 本篇讲解一个有些新颖的SharePoint实例应用,给甲方做过项目的都有过体会,数据太多了,客户有Excel,要求实现批量导入. 效 ...