ptyhon で組み合わせの総数を求める方法
ptyhon で組み合わせの総数を求める方法(2通り
①組み合わせを列挙→リスト化→リストの長さを求める
ライブラリitertoolsを使用
from itertools import combinations print ( len(list(combinations(range(a),b))) )
combinations(range(a),b)でa個の配列からb個選ぶ配列の組み合わせ作成
→それをlistでリスト化
→lenで総数を求める
②順列組み合わせを使って関数作成
ライブラリmathを使用
def c_count(n, r): return math.factorial(n) // (math.factorial(r)*math.factorial(n - r)) print (c_count(a,b))
階乗を返す関数math.factorial()を使って
n! / (r!(n - r)!)を返す関数作成
列挙をするなら①、処理コストなら②を選ぶといいかな?