Python

How to use Python for Permutations and Combinations?

How to use Python for Permutations and Combinations?

Python string permutations

The permutations() function makes it simple to complete a task like discovering every possible arrangement of the letters in a Python string.

EXAMPLE

import itertools

st = "ABC"

per = itertools.permutations(st)

for val in per:
    print(*val)

write your code here: Coding Playground

OUTPUT

A B C
A C B
B A C
B C A
C A B
C B A

The itertools object is returned by the function permutations() in exchange for a String parameter. If we attempt to print the variable "per" directly, we will obtain the results shown below:

<itertools.permutations object at 0x7fc9abdd8308>

So, in order to print each entry, a loop must be run.

Multiple-number permutations

We must supply the numbers as a list, set, or tuple in order to find their permutations of them because the permutations() function accepts an iterable input.

EXAMPLE

import itertools

values = [1, 2, 3]

per = itertools.permutations(values)

for val in per:
    print(*val)

write your code here: Coding Playground

OUTPUT

1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1

We include all the digits or characters in the permutation calculation methods mentioned above. The permutations can have a maximum or a minimum number of elements.

Combinations with a specific number of components

This can be done by passing an integer after the set of elements, much like the concept of "nPr," which states "Arranging r elements out of n."

EXAMPLE

import itertools

values = [1, 2, 3, 4]

per = itertools.permutations(values, 2)

for val in per:
    print(*val)

write your code here: Coding Playground

OUTPUT

1 2

1 3
1 4
2 1
2 3
2 4
3 1
3 2
3 4
4 1
4 2
4 3

The permutations() method is instructed to arrange only two elements at a time from the given list of integers in the code snippet above.

Words with multiple letter combinations

combinations() is the function to use if you need to find all combinations that contain exactly 2 letters from a given word.

EXAMPLE

import itertools

st = "ABCDE"

com = itertools.combinations(st, 2)

for val in com:
    print(*val)

write your code here: Coding Playground

OUTPUT

A B
A C
A D
A E
B C
B D
B E
C D
C E
D E

Sets of numbers combined

It is possible to achieve numbers in a list, similar to the combos result we got for letters in a word.

Example

import itertools

values = [1, 2, 3, 4]

com = itertools.combinations(values, 2)

for val in com:
    print(*val)

write your code here: Coding Playground

OUTPUT

1 2
1 3
1 4
2 3
2 4
3 4

Repetition of numerical combinations

Let's run an example of the aforementioned Note to further illustrate it.

EXAMPLE

import itertools

values = [1, 1, 2, 2]

com = itertools.combinations(values, 2)

for val in com:
    print(*val)

write your code here: Coding Playground

OUTPUT

1 1
1 2
1 2
1 2
1 2
2 2

Given that the numbers in the list are repeated, the outcome is obvious.

Number combinations that include themselves

Combinations with replacement is a further function in the itertools package that relates to permutations and combinations (). This function is a variant of combinations(), with the minor difference that it also takes into account combinations of the elements with each other.

EXAMPLE

import itertools

values = [1, 2, 3, 4]

com = itertools.combinations_with_replacement(values, 2)

for val in com:
    print(*val)

write your code here: Coding Playground

OUTPUT

1 1
1 2
1 3
1 4
2 2
2 3
2 4
3 3
3 4
4 4

When identical arguments are supplied to the combinations() function, we can clearly distinguish between the output from the above query and the output from that query.

Numbers can be combined with one another such that more than one instance of them appears in the list. This basically occurs because the aforementioned function sets the same value when we select an element from the list in order to create combinations with other elements.