不管在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. Struts+Spring+Hibernate整合入门详解

    Java 5.0 Struts 2.0.9 Spring 2.0.6 Hibernate 3.2.4 作者:  Liu Liu 转载请注明出处 基本概念和典型实用例子. 一.基本概念       St ...

  2. intel的网卡故障

    现象: 机器键盘接入,敲入无反应:机器无法ping通,整台机器假死状态. 查看message的日志,日志为如下内容: Aug :: TSMIS-CF kernel: ::19.0: eth0: Det ...

  3. Windows内核对象

    1. 内核对象 Windows中每个内核对象都只是一个内存块,它由操作系统内核分配,并只能由操作系统内核进行访问,应用程序不能在内存中定位这些数据结构并直接更改其内容.这个内存块是一个数据结构,其成员 ...

  4. 绑定本地Service并与之通信-----之二

    import android.os.Bundle;import android.os.IBinder;import android.app.Activity;import android.app.Se ...

  5. RHEL 6.3安装(超级详细图解教程)[转载]

        附:RHEL6.3下载地址 32位:http://rhel.ieesee.net/uingei/rhel-server-6.3-i386-dvd.iso 64位:http://rhel.iee ...

  6. USB鼠标按键驱动

    现象:把USB设备接到PC 1. 右下角弹出"发现android phone" 2. 跳出一个对话框,提示你安装驱动程序 问1. 既然还没有"驱动程序",为何能 ...

  7. Servlet下

    HTTP简介 HTTP是 hypertext transfer protocol(超文本传输协议)的简写,它是 TCP/IP 协议集中的一个应用层协议,用于定义WEB浏览器与WEB服务器之间交换数据的 ...

  8. Codeforces 235C

    题目大意: 给定一个字符串,接下来再给n个字符串,求原字符串中含有多少个当前给定字符串的循环同构体的字符串的个数 以初始字符串构建后缀自动机,在自动机上前进的时候,比如当前需要匹配的字符串为aba,到 ...

  9. Oracle中varchar,varchar2,nvarchar,nvarchar2的区别

    --varchar,varchar2 联系:1.varchar/varchar2用于存储可变长度的字符串比如varchar(20),存入字符串'abc',则数据库中该字段只占3个字节,而不是20个字节 ...

  10. UML学习入门就这一篇文章

    1.1 UML基础知识扫盲 UML这三个字母的全称是Unified Modeling Language,直接翻译就是统一建模语言,简单地说就是一种有特殊用途的语言. 你可能会问:这明明是一种图形,为什 ...