1. 程式人生 > >python 實現排列組合

python 實現排列組合

根據 ber number 數據 python Language img nat pri

1.python語言簡單、方便,其內部可以快速實現排列組合算法,下面做簡單介紹、

2.一個列表數據任意組合

2.1主要是利用自帶的庫

#_*_ coding:utf-8 _*_
#__author__=‘dragon‘
import itertools
list1 = [1,2,3,4,5]
list2 = []
for i in range(1,len(list1)+1):
    iter = itertools.combinations(list1,i)
    list2.append(list(iter))
print(list2)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

技術分享

3.排列的實現

#_*_ coding:utf-8 _*_
#__author__=‘dragon‘
import itertools
list1 = [1,2,3,4,5]
list2 = []
for i in range(1,len(list1)+1):
    iter = itertools.permutations(list1,i)
    list2.append(list(iter))
print(list2)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

技術分享

可以根據你需要隨意組合

python 實現排列組合