本文實例講述了Python實現的排列組合計算操作。分享給大家供大家參考,具體如下:
1. 調用 scipy 計算排列組合的具體數值
1
2
3
4
5
|
>> from scipy.special import comb, perm >> perm( 3 , 2 ) 6.0 >> comb( 3 , 2 ) 3.0 |
2. 調用 itertools 獲取排列組合的全部情況數
1
2
3
4
5
6
7
8
|
>> from itertools import combinations, permutations >> permutations([ 1 , 2 , 3 ], 2 ) <itertools.permutations at 0x7febfd880fc0 > # 可迭代對象 >> list (permutations([ 1 , 2 , 3 ], 2 )) [( 1 , 2 ), ( 1 , 3 ), ( 2 , 1 ), ( 2 , 3 ), ( 3 , 1 ), ( 3 , 2 )] >> list (combinations([ 1 , 2 , 3 ], 2 )) [( 1 , 2 ), ( 1 , 3 ), ( 2 , 3 )] |
希望本文所述對大家Python程序設計有所幫助。
原文鏈接:http://blog.csdn.net/lanchunhui/article/details/51824602