Python math.comb()メソッド
例
n個のアイテムからk個を選択する可能性の総数を見つけます。
# Import math Library
import math
# Initialize the number of
items to choose from
n = 7
# Initialize the number of possibilities
to choose
k = 5
# Print total number of possible combinations
print
(math.comb(n, k))
結果は次のようになります。
21
定義と使用法
このメソッドは、組み合わせとしても知られる、繰り返しなしで、n個の可能性からk個の順序付けられていない結果をmath.comb()
選択する方法の数を返します
。
注:このメソッドで渡されるパラメーターは、正の整数である必要があります。
構文
math.comb(n, k)
パラメータ値
Parameter | Description |
---|---|
n | Required. Positive integers of items to choose from |
k | Required. Positive integers of items to choose |
注:kの値がnの値より大きい場合、結果として0が返されます。
注:パラメーターが負の場合、ValueErrorが発生します。パラメータが整数でない場合、TypeErrorが発生します。
技術的な詳細
戻り値: | 組み合わせのint 総数を表す値 |
---|---|
Pythonバージョン: | 3.8 |