Python全排列操作實(shí)例分析
本文實(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ì)于自由變量,尚沒有類似的機(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)庫
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入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
python plt可視化——打印特殊符號(hào)和制作圖例代碼
這篇文章主要介紹了python plt可視化——打印特殊符號(hào)和制作圖例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-04-04Python實(shí)現(xiàn)二叉樹前序、中序、后序及層次遍歷示例代碼
這篇文章主要給大家介紹了關(guān)于Python實(shí)現(xiàn)二叉樹前序、中序、后序及層次遍歷的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Python具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05在Python3 numpy中mean和average的區(qū)別詳解
今天小編就為大家分享一篇在Python3 numpy中mean和average的區(qū)別詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-08-08Python3基礎(chǔ)之基本數(shù)據(jù)類型概述
這篇文章主要介紹了Python3的基本數(shù)據(jù)類型,需要的朋友可以參考下2014-08-08Python MySQLdb模塊連接操作mysql數(shù)據(jù)庫實(shí)例
這篇文章主要介紹了Python MySQLdb模塊連接操作mysql數(shù)據(jù)庫實(shí)例,本文直接給出操作mysql代碼實(shí)例,包含創(chuàng)建表、插入數(shù)據(jù)、插入多條數(shù)據(jù)、查詢數(shù)據(jù)等內(nèi)容,需要的朋友可以參考下2015-04-04Python轉(zhuǎn)換itertools.chain對(duì)象為數(shù)組的方法
這篇文章主要介紹了Python轉(zhuǎn)換itertools.chain對(duì)象為數(shù)組的方法,通過代碼給大家介紹了itertools 的 chain() 方法,需要的朋友可以參考下2020-02-02