Itertool is used for the list of combinations, but it took a long time to calculate the total number because it did not appear at the top of the search, so I will write it down here.
Use scipy.special.comb. Click here for details (https://docs.scipy.org/doc/scipy/reference/generated/scipy.special.comb.html)
from scipy.special import comb
[Execution example]
print(comb(4, 2)) #4C2 calculation
print(comb([4 ,3], [2, 1])) #You can also pass in list format, in this case you can calculate 4C2 and 3C1
【Execution result】
6.0
[6. 3.]
_nC_r = \frac{n!}{r!(n-r)!}
Create your own function using
import math
def combination(n,r):
return math.factorial(n)/math.factorial(r)/math.factorial(n-r)
[Execution example]
print(combination(4,2)) #4C2 calculation
【Execution result】
6.0
Recommended Posts