Permutation and Combination in Python:

In Python, we have direct methods for finding the permutation and combination of a sequence for data science. We can find the methods in the itertools package.

Permutation:

We need to import the itertools package first for implementing the permutation method in Python. The method will take a list as an input and will return an object list of tuples containing all the permutations in list form.

Permutation and Combination in Python for Data Science - PST Analytics

It will generate n! permutations in case the input sequence is n.

In case we want to find permutation of length L then we will implement it in the following manner.

Permutation and Combination in Python for Data Science - PST Analytics

It will generate nCr*r! permutations in case the length of the input sequence is n, and the input parameter is r.

Combination:

It will take a list and a input r. It returns an object list of tuples containing all the possible combinations of length r in list form.

  1. When we use combinations, it will be emitted in lexicographic sort order of input. Therefore if the list we input is sorted, the output will also be sorted.Permutation and Combination in Python for Data Science - PST Analytics
  2. Elements will be treated uniquely on the basis of their position and not on their value. Therefore if the input elements are unique, we won’t have repeated values in each combination.
  3. In case we want to make combination of the same element with the same element we will use combinations_with_replacement.

So, to learn more about permutations and combinations and how to implement it in python for data science, you can check this and this as well.

Leave a Reply

Your email address will not be published. Required fields are marked *