[Itertools.permutations] How to put permutations in Python

motivation

When I was doing AtCoder problem and wanted to get the "number in dictionary order", I had to find the permutation. For example, suppose you make a word using a, b, c, d, e. This time,

I learned how to do it, so I made a memorandum.

environment

manner

Very easy with itertools permutations In the code below, create a word using a, b, c, d, e and check what number "abcde", "deabc" is.

Code


import itertools

abcde_list = ["a", "b", "c", "d", "e"]
A = list(itertools.permutations(abcde_list))
# →[('a', 'b', 'c', 'd', 'e'), ('a', 'b', 'c', 'e', 'd'), ('a', 'b', 'd', 'c', 'e'), ('a', 'b', 'd', 'e', 'c'), ('a', 'b', 'e', 'c', 'd'), ('a', 'b', 'e', 'd', 'c'), ('a', 'c', 'b', 'd', 'e').....

#Note that it is a tuple
abcde_tuple = ("a", "b", "c", "d", "e")
deabc_tuple = ("d", "e", "a", "b", "c")

abcde_num = (A.index(abcde_tuple))
# →0
deabc_num = (A.index(deabc_tuple))
# →90

Note that *** index starts from 0. *** ***

Recommended Posts

[Itertools.permutations] How to put permutations in Python
How to generate permutations in Python and C ++
How to develop in Python
How to collect images in Python
How to use SQLite in Python
How to use Mysql in python
How to wrap C in Python
How to use ChemSpider in Python
How to use PubChem in Python
How to handle Japanese in Python
[Introduction to Python] How to use class in Python?
How to dynamically define variables in Python
How to do R chartr () in Python
PUT gzip directly to S3 in Python
How to work with BigQuery in Python
How to get a stacktrace in python
How to display multiplication table in python
How to extract polygon area in Python
How to check opencv version in python
How to switch python versions in cloud9
How to adjust image contrast in Python
How to use __slots__ in Python class
How to dynamically zero pad in Python
How to use regular expressions in Python
How to display Hello world in python
How to use is and == in Python
How to write Ruby to_s in Python
How to use python put in pyenv on macOS with PyCall
How to install Python
How to install python
How to use the C library in Python
How to receive command line arguments in Python
[REAPER] How to play with Reascript in Python
How to clear tuples in a list (Python)
How to embed a variable in a python string
Summary of how to import files in Python 3
How to simplify restricted polynomial fit in python
How to use Python Image Library in python3 series
How to implement shared memory in Python (mmap.mmap)
How to create a JSON file in Python
3 steps to put Python + mecab in yum only
Summary of how to use MNIST in Python
How to specify TLS version in python requests
How to create data to put in CNN (Chainer)
To add a module to python put in Julialang
How to notify a Discord channel in Python
How to get the files in the [Python] folder
How to use tkinter with python in pyenv
How to run Leap Motion in non-Apple Python
[Python] How to draw a histogram in Matplotlib
How to output "Ketsumaimo" as standard output in Python
How to handle datetime type in python sqlite3
How to make Python Interpreter changes in Pycharm
Sample to put Python Kivy in one file
How to plot autocorrelation and partial autocorrelation in python
How to remove duplicate elements in Python3 list
[Python] How to put any number of standard inputs in a list
How to put a half-width space before letters and numbers in Python.
20th Offline Real-time How to Write Problems in Python
[2020.8 latest] How to install Python
[For beginners] How to use say command in python!