2022
我们一起努力

Python中怎么实现全排列操作 - 开发技术

这篇文章给大家介绍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标准库

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中怎么实现全排列操作就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

赞(0)
文章名称:《Python中怎么实现全排列操作 - 开发技术》
文章链接:https://www.fzvps.com/80835.html
本站文章来源于互联网,如有侵权,请联系管理删除,本站资源仅供个人学习交流,请于下载后24小时内删除,不允许用于商业用途,否则法律问题自行承担。
图片版权归属各自创作者所有,图片水印出于防止被无耻之徒盗取劳动成果的目的。

评论 抢沙发

评论前必须登录!