溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Python中怎么實(shí)現(xiàn)全排列操作

發(fā)布時(shí)間:2021-06-15 16:11:24 來源:億速云 閱讀:329 作者:Leah 欄目:開發(fā)技術(shù)

這篇文章給大家介紹Python中怎么實(shí)現(xiàn)全排列操作,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。

step 1: 列表的全排列:

這個(gè)版本比較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

調(diào)用:

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

運(yùn)行結(jié)果:

[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 #記錄全排列的總數(shù)
  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

備注:

在閉包中,內(nèi)部函數(shù)依然維持了外部函數(shù)中自由變量的引用—單元。內(nèi)部函數(shù)不能修改單元對(duì)象的值(但是可以引用)。若嘗試修改,則解釋器會(huì)認(rèn)為它是局部變量。這類似于全局變量和局部變量的關(guān)系。如果在函數(shù)內(nèi)部修改全局變量,必須加上global聲明,但是對(duì)于自由變量,尚沒有類似的機(jī)制。所以,只能使用列表。(python3中引入了關(guān)鍵字:nonlocal)

測(cè)試:

permutation('abcd')

運(yùn)行結(jié)果:

['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標(biāo)準(zhǔn)庫(kù)

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

運(yùn)行結(jié)果:

[(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)]

可以指定排列的位數(shù):

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

運(yùn)行結(jié)果:

[(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)]

關(guān)于Python中怎么實(shí)現(xiàn)全排列操作就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI