Python實(shí)現(xiàn)全排列的打印
本文為大家分享了Python實(shí)現(xiàn)全排列的打印的代碼,供大家參考,具體如下
問(wèn)題:輸入一個(gè)數(shù)字:3,打印它的全排列組合:123 132 213 231 312 321,并進(jìn)行統(tǒng)計(jì)個(gè)數(shù)。
下面是Python的實(shí)現(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):
'''遞歸實(shí)現(xiàn)交換其中的兩個(gè)。一直循環(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
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python使用zmail進(jìn)行郵件發(fā)送的示例詳解
這篇文章主要為大家詳細(xì)介紹了Python如何使用zmail進(jìn)行郵件發(fā)送功能,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,有需要的小伙伴可以參考一下2024-03-03
python中g(shù)etaddrinfo()基本用法實(shí)例分析
這篇文章主要介紹了python中g(shù)etaddrinfo()基本用法,實(shí)例分析了Python中使用getaddrinfo方法進(jìn)行IP地址解析的基本技巧,需要的朋友可以參考下2015-06-06
分析python并發(fā)網(wǎng)絡(luò)通信模型
隨著互聯(lián)網(wǎng)和物聯(lián)網(wǎng)的高速發(fā)展,使用網(wǎng)絡(luò)的人數(shù)和電子設(shè)備的數(shù)量急劇增長(zhǎng),其也對(duì)互聯(lián)網(wǎng)后臺(tái)服務(wù)程序提出了更高的性能和并發(fā)要求。本文主要分析比較了一些模型的優(yōu)缺點(diǎn),并且用python來(lái)實(shí)現(xiàn)2021-06-06
python sklearn常用分類算法模型的調(diào)用
這篇文章主要介紹了python sklearn常用分類算法模型的調(diào)用,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-10-10
python爬蟲(chóng)通過(guò)增加多線程獲取數(shù)據(jù)
這篇文章主要為大家介紹了python爬蟲(chóng)通過(guò)增加多線程獲取數(shù)據(jù)實(shí)現(xiàn)過(guò)程解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
Python+numpy實(shí)現(xiàn)矩陣的行列擴(kuò)展方式
今天小編就為大家分享一篇Python+numpy實(shí)現(xiàn)矩陣的行列擴(kuò)展方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11
Python 使用SMOTE解決數(shù)據(jù)不平衡問(wèn)題(最新推薦)
SMOTE是一種強(qiáng)大的過(guò)采樣技術(shù),可以有效地處理不平衡數(shù)據(jù)集,提升分類器的性能,通過(guò)imbalanced-learn庫(kù)中的SMOTE實(shí)現(xiàn),我們可以輕松地對(duì)少數(shù)類樣本進(jìn)行過(guò)采樣,平衡數(shù)據(jù)集,這篇文章主要介紹了Python 使用SMOTE解決數(shù)據(jù)不平衡問(wèn)題,需要的朋友可以參考下2024-05-05
Python實(shí)現(xiàn)subprocess執(zhí)行外部命令
Python使用最廣泛的是標(biāo)準(zhǔn)庫(kù)的subprocess模塊,使用subprocess最簡(jiǎn)單的方式就是用它提供的便利函數(shù),因此執(zhí)行外部命令優(yōu)先使用subprocess模塊,下面就一起來(lái)了解一下如何使用2021-05-05

