Python全排列操作实例分析

作者:wanlifeipeng 时间:2023-08-24 20:17:54 

本文实例讲述了Python全排列操作。分享给大家供大家参考,具体如下:

step 1: 列表的全排列:

这个版本比较low


# -*-coding:utf-8 -*-
#!python3
def permutation(li,index):
 for i in range(index,len(li)):
   if index == len(li)-1:
     print(li)
     return
   tmp = li[index]
   li[index] = li[i]
   li[i] = tmp
   permutation(li,index+1)
   tmp = li[index]
   li[index] = li[i]
   li[i] = tmp

调用:


permutation([1,2,3,4],0)

运行结果:

[1, 2, 3, 4]
[1, 2, 4, 3]
[1, 3, 2, 4]
[1, 3, 4, 2]
[1, 4, 3, 2]
[1, 4, 2, 3]
[2, 1, 3, 4]
[2, 1, 4, 3]
[2, 3, 1, 4]
[2, 3, 4, 1]
[2, 4, 3, 1]
[2, 4, 1, 3]
[3, 2, 1, 4]
[3, 2, 4, 1]
[3, 1, 2, 4]
[3, 1, 4, 2]
[3, 4, 1, 2]
[3, 4, 2, 1]
[4, 2, 3, 1]
[4, 2, 1, 3]
[4, 3, 2, 1]
[4, 3, 1, 2]
[4, 1, 3, 2]
[4, 1, 2, 3]

step2: 字符串的全排列:


# -*-coding:utf-8 -*-
#!python3
def permutation(str):
 li = list(str)
 cnt = 0 #记录全排列的总数
 def permutation_list(index):
   if index == len(li) -1:
     nonlocal cnt
     cnt += 1
     print(li)
   for i in range(index,len(li)):
     li[index],li[i] = li[i],li[index]
     permutation_list(index+1)
     li[index], li[i] = li[i], li[index]
 ret = permutation_list(0)
 print("共有%d中全排列" % cnt)
 return ret

备注:

在闭包中,内部函数依然维持了外部函数中自由变量的引用—单元。内部函数不能修改单元对象的值(但是可以引用)。若尝试修改,则解释器会认为它是局部变量。这类似于全局变量和局部变量的关系。如果在函数内部修改全局变量,必须加上global声明,但是对于自由变量,尚没有类似的机制。所以,只能使用列表。(python3中引入了关键字:nonlocal)

测试:


permutation('abcd')

运行结果:

['a', 'b', 'c', 'd']
['a', 'b', 'd', 'c']
['a', 'c', 'b', 'd']
['a', 'c', 'd', 'b']
['a', 'd', 'c', 'b']
['a', 'd', 'b', 'c']
['b', 'a', 'c', 'd']
['b', 'a', 'd', 'c']
['b', 'c', 'a', 'd']
['b', 'c', 'd', 'a']
['b', 'd', 'c', 'a']
['b', 'd', 'a', 'c']
['c', 'b', 'a', 'd']
['c', 'b', 'd', 'a']
['c', 'a', 'b', 'd']
['c', 'a', 'd', 'b']
['c', 'd', 'a', 'b']
['c', 'd', 'b', 'a']
['d', 'b', 'c', 'a']
['d', 'b', 'a', 'c']
['d', 'c', 'b', 'a']
['d', 'c', 'a', 'b']
['d', 'a', 'c', 'b']
['d', 'a', 'b', 'c']
共有24中全排列

step3 : 使用python标准库


import itertools
t = list(itertools.permutations([1,2,3,4]))
print(t)

运行结果:

[(1, 2, 3, 4), (1, 2, 4, 3), (1, 3, 2, 4), (1, 3, 4, 2), (1, 4, 2, 3), (1, 4, 3, 2), (2, 1, 3, 4), (2, 1, 4, 3), (2, 3, 1, 4), (2, 3, 4, 1), (2, 4, 1, 3), (2, 4, 3, 1), (3, 1, 2, 4), (3, 1, 4, 2), (3, 2, 1, 4), (3, 2, 4, 1), (3, 4, 1, 2), (3, 4, 2, 1), (4, 1, 2, 3), (4, 1, 3, 2), (4, 2, 1, 3), (4, 2, 3, 1), (4, 3, 1, 2), (4, 3, 2, 1)]

可以指定排列的位数:


import itertools
t = itertools.permutations([1,2,3,4],3) #只排列3位
print(list(t))

运行结果:

[(1, 2, 3), (1, 2, 4), (1, 3, 2), (1, 3, 4), (1, 4, 2), (1, 4, 3), (2, 1, 3), (2, 1, 4), (2, 3, 1), (2, 3, 4), (2, 4, 1), (2, 4, 3), (3, 1, 2), (3, 1, 4), (3, 2, 1), (3, 2, 4), (3, 4, 1), (3, 4, 2), (4, 1, 2), (4, 1, 3), (4, 2, 1), (4, 2, 3), (4, 3, 1), (4, 3, 2)]

希望本文所述对大家Python程序设计有所帮助。

来源:http://www.cnblogs.com/hupeng1234/p/6681275.html

标签:Python,全排列
0
投稿

猜你喜欢

  • python asyncio 协程库的使用

    2022-09-13 13:28:16
  • citespace数据处理:用python对Ref文档进行去重方式

    2022-07-06 06:01:13
  • 详解Python中@staticmethod和@classmethod区别及使用示例代码

    2023-06-03 12:17:20
  • 详解python第三方库的安装、PyInstaller库、random库

    2023-03-13 06:57:46
  • 2个asp防刷新程序代码

    2008-09-28 21:16:00
  • Python中使用items()方法返回字典元素对的教程

    2023-11-20 13:07:09
  • asp中的rs.open与conn.execute的区别说明

    2011-02-24 10:56:00
  • 在asp中用集合成批操作数据库

    2008-03-10 17:22:00
  • 在VS2017中用C#调用python脚本的实现

    2021-09-19 00:59:06
  • IE下中英文字体不能对齐原因及解决

    2008-08-11 12:47:00
  • ASP开发10条经验总结

    2007-09-30 13:36:00
  • 如何高效地访问记录集?

    2009-11-22 19:25:00
  • Python爬虫番外篇之Cookie和Session详解

    2022-02-09 18:56:44
  • Python获取任意xml节点值的方法

    2023-03-09 01:40:07
  • 搞定MySQL数据库中文模糊检索问题

    2007-09-17 12:36:00
  • 深入讲解Python函数中参数的使用及默认参数的陷阱

    2022-04-21 20:09:14
  • 通过T-SQL语句实现数据库备份与还原的代码

    2011-12-01 08:02:15
  • python二分法查找算法实现方法【递归与非递归】

    2023-04-17 08:13:47
  • 浅谈python print(xx, flush = True) 全网最清晰的解释

    2022-01-28 21:45:48
  • django框架如何集成celery进行开发

    2022-09-13 11:18:24
  • asp之家 网络编程 m.aspxhome.com