Statistics: combinations in Python

前端 未结 18 1592
南旧
南旧 2020-11-27 10:14

I need to compute combinatorials (nCr) in Python but cannot find the function to do that in math, numpy or stat libraries. Something

18条回答
  •  情书的邮戳
    2020-11-27 10:46

    Starting Python 3.8, the standard library now includes the math.comb function to compute the binomial coefficient:

    math.comb(n, k)

    which is the number of ways to choose k items from n items without repetition
    n! / (k! (n - k)!):

    import math
    math.comb(10, 5) # 252
    

提交回复
热议问题