Combined with permutations in Python

In Python, permutations and combinations can be displayed and calculated really intuitively, which is convenient.

reference

Permutations (P: Permutations)

The pattern of arrangement when the five elements a, b, c, d, and e meet is 5! (Factial factorial of 5). In other words

_5 P _5 = 5! = 5 * 4 * 3 * 2 * 1 = 120

To find this in Python

#coding:utf-8
import itertools

#Target to line up
s = ['a','b','c','d','e']

#list
p = list(itertools.permutations(s));

#Pattern display
#print p

#Number of patterns displayed
print len(p)

And. If you choose 3 out of 5 and arrange them

_5 P _3 = 5 * 4 * 3 = 60

Will be. If you want this in Python

p = list(itertools.permutations(s,3));

(Excerpt from the above).

Combinations (C: Combinations)

Then permutation. Regardless of the order, if the elements are the same, they are counted as one. For example, (a, b, c) and (a, c, b) are considered to be one. Similar to the above, if you select 3 from a, b, c, d, e, the combination is

_5 C _3 = \frac{_5 P _3}{3!} = \frac{5 * 4 * 3}{3 * 2 * 1} =  10

Will be. To calculate this in Python

#coding:utf-8
import itertools

#Target to line up
s = ['a','b','c','d','e']

#list
c = list(itertools.combinations(s,3));

#Pattern display
#print c

#Number of patterns displayed
print len(c)

And.

Recommended Posts

Combined with permutations in Python
Scraping with selenium in Python
Scraping with chromedriver in python
Debugging with pdb in Python
Working with sounds in Python
Scraping with Selenium in Python
Find permutations / combinations in Python
Scraping with Tor in Python
Tweet with image in Python
Number recognition in images with Python
Testing with random numbers in Python
GOTO in Python with Sublime Text 3
Working with LibreOffice in Python: import
Scraping with Selenium in Python (Basic)
CSS parsing with cssutils in Python
Numer0n with items made in Python
Open UTF-8 with BOM in Python
Use rospy with virtualenv in Python3
Use Python in pyenv with NeoVim
Heatmap with Dendrogram in Python + matplotlib
Read files in parallel with Python
Password generation in texto with python
Use OpenCV with Python 3 in Window
Until dealing with python in Atom
Get started with Python in Blender
Working with DICOM images in Python
Quadtree in Python --2
Python in optimization
CURL in python
FizzBuzz with Python3
Metaprogramming in Python
Spiral book in Python! Python with a spiral book! (Chapter 14 ~)
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Scraping with Python
Try logging in to qiita with Python
Stress Test with Locust written in Python
Python3> in keyword> True with partial match?
Statistics with python
Device monitoring with On-box Python in IOS-XE
Meta-analysis in Python
Unittest in python
Scraping with Python
Python with Go
Try working with binary data in Python
[Itertools.permutations] How to put permutations in Python
Draw Nozomi Sasaki in Excel with python
Tips for dealing with binaries in Python
Display Python 3 in the browser with MAMP
Page cache in Python + Flask with Flask-Caching
Twilio with Python
Epoch in Python
Integrate with Python
Post Test 3 (Working with PosgreSQL in Python)
How to work with BigQuery in Python
Playing card class in Python (with comparison)
Sudoku in Python
DCI in Python
Play with 2016-Python
quicksort in python