如何在 Python 中使用 SciPy 計算排列和組合值?


SciPy 可用於確定關於兩個值的排列和組合。

使用 SciPy 中的 'special' 類中的名為 'perm' 的函式。

‘perm’ 函式的語法

scipy.special.perm(N, k)

下面顯示了對一組值執行排列。

示例

 線上演示

from scipy.special import perm
my_permute = perm(6, 2, exact = True)
print("The permutation of 6 and 2 is ")
print(my_permute)

輸出

The permutation of 6 and 2 is
30

解釋

  • 匯入所需的庫。
  • 將引數傳遞給計算值的 'perm' 函式。
  • 將值分配給變數。
  • 此變數在控制檯上顯示。

在 SciPy 中計算兩個值的組合

使用 SciPy 中的 'special' 類中的名為 'comb' 的函式。

‘comb’ 函式的語法

scipy.special.comb(N, k)

下面顯示了對一組值執行排列。

示例

 線上演示

from scipy.special import comb
my_comb = comb(6, 2, exact = True)
print("The combination of 6 and 2 is ")
print(my_comb)

輸出

The combination of 6 and 2 is
15

解釋

  • 匯入所需的庫。
  • 將引數傳遞給計算值的 'comb' 函式。
  • 將值分配給變數。
  • 此變數在控制檯上顯示。

更新於: 2020-12-11

540 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.