国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python全排列操作實例分析

2020-01-04 14:44:24
字體:
來源:轉載
供稿:網友

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

step 1: 列表的全排列:

這個版本比較low

# -*-coding:utf-8 -*-#!python3def 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

調用:

permutation([1,2,3,4],0)

運行結果:

[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 -*-#!python3def permutation(str):  li = list(str)  cnt = 0 #記錄全排列的總數  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

備注:

在閉包中,內部函數依然維持了外部函數中自由變量的引用—單元。內部函數不能修改單元對象的值(但是可以引用)。若嘗試修改,則解釋器會認為它是局部變量。這類似于全局變量和局部變量的關系。如果在函數內部修改全局變量,必須加上global聲明,但是對于自由變量,尚沒有類似的機制。所以,只能使用列表。(python3中引入了關鍵字:nonlocal)

測試:

permutation('abcd')

運行結果:

['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標準庫

import itertoolst = list(itertools.permutations([1,2,3,4]))print(t)

運行結果:

[(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)]

可以指定排列的位數:

import itertoolst = itertools.permutations([1,2,3,4],3) #只排列3位print(list(t))

運行結果:

[(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)]

希望本文所述對大家Python程序設計有所幫助。


注:相關教程知識閱讀請移步到python教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 镇坪县| 开远市| 湘潭市| 阿克苏市| 丽江市| 大悟县| 乌兰察布市| 加查县| 巢湖市| 巴南区| 拜泉县| 剑川县| 德格县| 兴义市| 英德市| 中牟县| 永春县| 杭锦旗| 礼泉县| 庆阳市| 蒙阴县| 政和县| 新野县| 黄陵县| 呼伦贝尔市| 邯郸县| 济南市| 韩城市| 勐海县| 恩施市| 涟源市| 儋州市| 泰安市| 湛江市| 江油市| 高要市| 慈利县| 晋宁县| 双牌县| 桦南县| 宜兰县|