Calculate the total number of combinations with python

Introduction

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 functions (recommended)

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.]

When creating your own function

_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

Calculate the total number of combinations with python
Calculate the number of changes
Calculate the regression coefficient of simple regression analysis with python
Calculate the square root of 2 in millions of digits with python
[Homology] Count the number of holes in data with Python
Check the existence of the file with python
Count the number of characters with echo
Tips: [Python] Calculate the average value of the specified area with bedgraph
Output the number of CPU cores in Python
Prepare the execution environment of Python3 with Docker
Calculate the shortest route of a graph with Dijkstra's algorithm and Python
2016 The University of Tokyo Mathematics Solved with Python
Get the number of searches with a regular expression. SeleniumBasic VBA Python
[Note] Export the html of the site with python.
the zen of Python
[Python] Calculate the number of digits required when filling in 0s [Note]
Check the date of the flag duty with Python
Calculate the probability of being a squid coin with Bayes' theorem [python]
Get the number of articles accessed and likes with Qiita API + Python
Convert the character code of the file with Python3
[Python] Determine the type of iris with SVM
[Python] Automatically totals the total number of articles posted by Qiita using the API
Align the number of samples between classes of data for machine learning with Python
Extract the table of image files with OneDrive & Python
[Python] A program that counts the number of valleys
How to get the number of digits in Python
Learn Nim with Python (from the beginning of the year).
[Python] Calculate the average value of the pixel value RGB of the object
Visualize the range of interpolation and extrapolation with python
Predict the number of people infected with COVID-19 with Prophet
Get the size (number of elements) of UnionFind in Python
Manage the package version number of requirements.txt with pip-tools
[Python] Outputs all combinations of elements in the list
Summary of the basic flow of machine learning with Python
Get the operation status of JR West with Python
[Python] Get the number of views of all posted articles
Extract the band information of raster data with python
Calculate the product of matrices with a character expression?
10. Counting the number of lines
Towards the retirement of Python2
About the ease of Python
Get the number of digits
ABC161D Lunlun Number with python3
Call the API with python3.
About the features of Python
The Power of Pandas: Python
How to identify the element with the smallest number of characters in a Python list?
An easy way to pad the number with zeros depending on the number of digits [Python]
How to count the number of occurrences of each element in the list in Python with weight
I tried to find the entropy of the image with python
Try scraping the data of COVID-19 in Tokyo with Python
I tried "gamma correction" of the image with Python + OpenCV
The story of implementing the popular Facebook Messenger Bot with python
Unify the environment of the Python development team starting with Poetry
Visualize the results of decision trees performed with Python scikit-learn
I wrote the basic grammar of Python with Jupyter Lab
Tank game made with python About the behavior of tanks
Run the intellisense of your own python library with VScode.
Get the number of specific elements in a python list
I evaluated the strategy of stock system trading with Python.
Python --Find out number of groups in the regex expression