Permutation and Combination in Python?

PythonServer Side ProgrammingProgramming

In this section, we are going to learn how to find permutation and combination of a given sequence using python programming language.

One of the key advantage of python over other programming language is that it comes with huge set of libraries with it.

We are going to use python inbuilt package to find permutation and combinations of a given sequence.

Algorithm to find the Permutation and combination

  • Step 1 : Import required package. First step is to import the required package, as we are going to use itertools package, so we just import it using.

>>> import itertools
>>>
  • Step 2: Get all permutation & combination of a sequence. Second step is to enter a list of sequences/items as an input that will return all permutation and combinations in the form of list of tuples.

  • We can also set the length of permutation and combination.

  • Step 3: Printing the result Last step is to print the all the permutation and combination of set of sequences. We can use the loop function to print the result.

Permutation

Let’s find the permutation of a list of three items.

Example 1

from itertools import permutations

seq = permutations(['a','b','c'])

for p in list(seq):
   print(p)

Result

('a', 'b', 'c')
('a', 'c', 'b')
('b', 'a', 'c')
('b', 'c', 'a')
('c', 'a', 'b')
('c', 'b', 'a')

Example 2:

Find the permutation by defining the length of the permutation.

from itertools import permutations

seq = permutations(['p', 'y', 't', 'h', 'o', 'n'], 2)

for p in list(seq):
   print(p)

Result

('p', 'y')
('p', 't')
('p', 'h')
('p', 'o')
('p', 'n')
('y', 'p')
('y', 't')
('y', 'h')
('y', 'o')
('y', 'n')
('t', 'p')
('t', 'y')
('t', 'h')
('t', 'o')
('t', 'n')
('h', 'p')
('h', 'y')
('h', 't')
('h', 'o')
('h', 'n')
('o', 'p')
('o', 'y')
('o', 't')
('o', 'h')
('o', 'n')
('n', 'p')
('n', 'y')
('n', 't')
('n', 'h')
('n', 'o')

Combinations

Let’s find the combination of a sequence using python.

Example 1: Determine the length of the combination

#Import itertools package
from itertools import combinations

#Getting all combination of a particular length.
combi = combinations(['p', 'y', 't', 'h', 'o', 'n'], 5)

#Print the list of combinations

for c in list(combi):
   print(c)

Result

('p', 'y', 't', 'h', 'o')
('p', 'y', 't', 'h', 'n')
('p', 'y', 't', 'o', 'n')
('p', 'y', 'h', 'o', 'n')
('p', 't', 'h', 'o', 'n')
('y', 't', 'h', 'o', 'n')

Example 2: Combinations with replacements

#Import itertools package
from itertools import combinations_with_replacement

#Getting all combination by defining a particular length.
combi = combinations_with_replacement(['p', 'y', 't', 'h', 'o', 'n'], 2)

#Print the list of combinations

for c in list(combi):
   print(c)

Result

('p', 'p')
('p', 'y')
('p', 't')
('p', 'h')
('p', 'o')
('p', 'n')
('y', 'y')
('y', 't')
('y', 'h')
('y', 'o')
('y', 'n')
('t', 't')
('t', 'h')
('t', 'o')
('t', 'n')
('h', 'h')
('h', 'o')
('h', 'n')
('o', 'o')
('o', 'n')
('n', 'n')
raja
Published on 02-May-2019 16:33:51
Advertisements