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
調(diào)用:
permutation([1,2,3,4],0)
運行結(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ù)不能修改單元對象的值(但是可以引用)。若嘗試修改,則解釋器會認(rèn)為它是局部變量。這類似于全局變量和局部變量的關(guān)系。如果在函數(shù)內(nèi)部修改全局變量,必須加上global聲明,但是對于自由變量,尚沒有類似的機制。所以,只能使用列表。(python3中引入了關(guān)鍵字:nonlocal)
測試:
permutation('abcd')
運行結(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)
運行結(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))
運行結(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é)運算技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設(shè)計有所幫助。
相關(guān)文章
Python操作Excel數(shù)據(jù)的封裝函數(shù)分享
對比其它編程語言,我們都知道Python最大的優(yōu)勢是代碼簡單,有豐富的第三方開源庫供開發(fā)者使用。而對于數(shù)據(jù)的讀取和存儲,對于普通人來講,除了數(shù)據(jù)庫之外,最常見的就是微軟的Excel。本文為大家準(zhǔn)備了Python操作Excel數(shù)據(jù)的封裝函數(shù),希望對大家有所幫助2022-11-11
python采集天氣數(shù)據(jù)并做數(shù)據(jù)可視化
本文主要介紹了python采集天氣數(shù)據(jù)并做數(shù)據(jù)可視化,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
Python基于當(dāng)前時間批量創(chuàng)建文件
這篇文章主要介紹了Python基于當(dāng)前時間批量創(chuàng)建文件,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-05-05

