Python實現(xiàn)全排列的打印
更新時間:2018年08月18日 10:12:57 作者:enciyetu
這篇文章主要為大家詳介紹了Python實現(xiàn)全排列的打印的相關(guān)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文為大家分享了Python實現(xiàn)全排列的打印的代碼,供大家參考,具體如下
問題:輸入一個數(shù)字:3,打印它的全排列組合:123 132 213 231 312 321,并進行統(tǒng)計個數(shù)。
下面是Python的實現(xiàn)代碼:
#!/usr/bin/env python
# -*- coding: <encoding name> -*-
'''
全排列的demo
input : 3
output:123 132 213 231 312 321
'''
total = 0
def permutationCove(startIndex, n, numList):
'''遞歸實現(xiàn)交換其中的兩個。一直循環(huán)下去,直至startIndex == n
'''
global total
if startIndex >= n:
total += 1
print numList
return
for item in range(startIndex, n):
numList[startIndex], numList[item] = numList[item], numList[startIndex]
permutationCove(startIndex + 1, n, numList )
numList[startIndex], numList[item] = numList[item], numList[startIndex]
n = int(raw_input("please input your number:"))
startIndex = 0
total = 0
numList = [x for x in range(1,n+1)]
print '*' * 20
for item in range(0, n):
numList[startIndex], numList[item] = numList[item], numList[startIndex]
permutationCove(startIndex + 1, n, numList)
numList[startIndex], numList[item] = numList[item], numList[startIndex]
print total
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python中g(shù)etaddrinfo()基本用法實例分析
這篇文章主要介紹了python中g(shù)etaddrinfo()基本用法,實例分析了Python中使用getaddrinfo方法進行IP地址解析的基本技巧,需要的朋友可以參考下2015-06-06
分析python并發(fā)網(wǎng)絡(luò)通信模型
隨著互聯(lián)網(wǎng)和物聯(lián)網(wǎng)的高速發(fā)展,使用網(wǎng)絡(luò)的人數(shù)和電子設(shè)備的數(shù)量急劇增長,其也對互聯(lián)網(wǎng)后臺服務程序提出了更高的性能和并發(fā)要求。本文主要分析比較了一些模型的優(yōu)缺點,并且用python來實現(xiàn)2021-06-06
python sklearn常用分類算法模型的調(diào)用
這篇文章主要介紹了python sklearn常用分類算法模型的調(diào)用,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-10-10
Python 使用SMOTE解決數(shù)據(jù)不平衡問題(最新推薦)
SMOTE是一種強大的過采樣技術(shù),可以有效地處理不平衡數(shù)據(jù)集,提升分類器的性能,通過imbalanced-learn庫中的SMOTE實現(xiàn),我們可以輕松地對少數(shù)類樣本進行過采樣,平衡數(shù)據(jù)集,這篇文章主要介紹了Python 使用SMOTE解決數(shù)據(jù)不平衡問題,需要的朋友可以參考下2024-05-05
Python實現(xiàn)subprocess執(zhí)行外部命令
Python使用最廣泛的是標準庫的subprocess模塊,使用subprocess最簡單的方式就是用它提供的便利函數(shù),因此執(zhí)行外部命令優(yōu)先使用subprocess模塊,下面就一起來了解一下如何使用2021-05-05

