Python 全排列

 


1 Python 排列功能

  比如 [1, 2, 3] 全排列有多少种?共计 3*2=6 种,那么用程序怎么实现呢?如果是 [1, 2, 3] 取两位数字,组成列表,又有几种?
  这里可以使用 Python 内置的 itertools 库的全排列 permutations 函数或者循环处理

1.1 permutations 

复制代码
from itertools import permutations


def permutation1(li=[1,2,3], n=None):
    if n and n<= len(li):
        return permutations(li, n)
    if not n:
        return permutations(li)


if __name__ == '__main__':
    li = ['a', 'b', 'c']
    per = list(permutation1(li))
    print(len(per), per)
    per1 = list(permutation1(li, 2))
    print(len(per1), per1)
复制代码

 

1 6 [('a', 'b', 'c'), ('a', 'c', 'b'), ('b', 'a', 'c'), ('b', 'c', 'a'), ('c', 'a', 'b'), ('c', 'b', 'a')]
2 6 [('a', 'b'), ('a', 'c'), ('b', 'a'), ('b', 'c'), ('c', 'a'), ('c', 'b')]
View Result

1.2 循环处理

  思路:
    对一个数进行排列时,结果只有一个:1
    对两个数进行排列时,将第二个数插进第一个排列的所有可能空位:2,1 和 1,2(前面和后面)
    对三个数进行排列时,将第三个数插进第二个排列的所有可能空位:
      将 3 插进 2,1:3,2,1 和 2,3,1 和 2,1,3(依次插进前中后)
      将 3 插进 1,2:3,1,2 和 1,3,2 和 1,2,3(依次插进前中后)
    对四个数进行排列时,将第四个数插进第三个排列的所有可能空位:
      将 4 插进 3,2,1:4321,3421,3241,3214
      将 4 插进 2,3,1:4231,2431,2341,2314
      ......以此类推

复制代码
 1 from copy import deepcopy
 2 
 3 def permutation2(li=[1,2,3]):
 4     """
 5     全排列
 6     思路:
 7     对一个数进行排列时,结果只有一个:1
 8     对两个数进行排列时,将第二个数插进第一个排列的所有可能空位:2,1 和 1,2(前面和后面)
 9     对三个数进行排列时,将第三个数插进第二个排列的所有可能空位:
10         将 3 插进 2,1:3,2,1 和 2,3,1 和 2,1,3(依次插进前中后)
11         将 3 插进 1,2:3,1,2 和 1,3,2 和 1,2,3(依次插进前中后)
12     对四个数进行排列时,将第四个数插进第三个排列的所有可能空位:
13         将 4 插进 3,2,1:4321,3421,3241,3214
14         将 4 插进 2,3,1:4231,2431,2341,2314
15         ......以此类推
16     :param li:
17     :return:
18     """
19     if li ==[]:
20         return []
21     all_per = [[li[0]]]  # 初始化列表
22     # print(all_per)
23     len_li = len(li)  # 列表长度
24     # print(len_li)
25     for i in range(1, len_li):  # 插入时,从第二个元素开始取值
26         up_per = []  # 空列表,储存每个元素插入时的所有可能
27         len1 = len(all_per)  # all_per 的长度
28         # print('len1: ', len1)
29         for j in range(len1):
30             # 插入位置,比如将 2 插入 [[1]] 时,有两种情况,在前或者后插入,因此可能是 [1].insert(0, 2) 或者 [1].insert(1, 2)
31             len2 = len(all_per[j]) + 1
32             # print('len2: ', len2)
33             for k in range(len2):
34                 per = deepcopy(all_per[j])  # 深拷贝,防止插入数据后影响 all_per[j]
35                 # print('per: ', per)
36                 per.insert(k, li[i])  # 插入数据
37                 up_per.append(per)  # 添加到 up_per 里面
38                 # print('up_per: ', up_per)
39         # 每次将所有可能插入完后,需要更新 all_per
40         # 比如将 2 插入 [[1]] 时,得到两种可能 [[2,1],[1,2]],这里需要用 [[2,1],[1,2]] 代替 [[1]]
41         all_per = up_per
42         # print('all_per: ', all_per)
43     return len(all_per), all_per
44 
45 
46 if __name__ == '__main__':
47     li = ['a', 'b', 'c']
48     per = permutation2(li)
49     print(per)
View Code
复制代码
1 [['c', 'b', 'a'], ['b', 'c', 'a'], ['b', 'a', 'c'], ['c', 'a', 'b'], ['a', 'c', 'b'], ['a', 'b', 'c']]
View Result

 

posted @   Lipx9527  阅读(1129)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示

目录导航