欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python全排列操作實(shí)例分析

 更新時(shí)間:2018年07月24日 14:58:56   作者:wanlifeipeng  
這篇文章主要介紹了Python全排列操作,結(jié)合實(shí)例形式分析了Python列表、字符串全排列算法,以及使用標(biāo)準(zhǔn)庫(kù)itertools進(jìn)行全排列的相關(guān)操作技巧,需要的朋友可以參考下

本文實(shí)例講述了Python全排列操作。分享給大家供大家參考,具體如下:

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ì)于自由變量,尚沒(méi)有類似的機(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相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python數(shù)學(xué)運(yùn)算技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門(mén)與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總

希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論