How to get combinations of numbers in python

I'm a bit late on this topic, but think I can help someone.

You can use product from itertools:

from itertools import product

n = [1, 2, 3]

result = product[n, repeat=3] # You can change the repeat more then n length

print[list[result]]

Output:

[[1, 1, 1], [1, 1, 2], [1, 1, 3], [1, 2, 1], [1, 2, 2], [1, 2, 3], [1, 3, 1],
 [1, 3, 2], [1, 3, 3], [2, 1, 1], [2, 1, 2], [2, 1, 3], [2, 2, 1], [2, 2, 2],
 [2, 2, 3], [2, 3, 1], [2, 3, 2], [2, 3, 3], [3, 1, 1], [3, 1, 2], [3, 1, 3], 
[3, 2, 1], [3, 2, 2], [3, 2, 3], [3, 3, 1], [3, 3, 2], [3, 3, 3]]

Another example, but changing repeat arguement:

from itertools import product

n = [1, 2, 3]

result = product[n, repeat=4] # Changing repeat to 4
print[list[result]]

Output:

[1, 1, 2, 3], [1, 1, 3, 1], [1, 1, 3, 2], [1, 1, 3, 3], [1, 2, 1, 1], 
[1, 2, 1, 2], [1, 2, 1, 3], [1, 2, 2, 1], [1, 2, 2, 2], [1, 2, 2, 3], 
[1, 2, 3, 1], [1, 2, 3, 2], [1, 2, 3, 3], [1, 3, 1, 1], [1, 3, 1, 2], 
[1, 3, 1, 3], [1, 3, 2, 1], [1, 3, 2, 2], [1, 3, 2, 3], [1, 3, 3, 1], 
[1, 3, 3, 2], [1, 3, 3, 3], [2, 1, 1, 1], [2, 1, 1, 2], [2, 1, 1, 3], 
[2, 1, 2, 1], [2, 1, 2, 2], [2, 1, 2, 3], [2, 1, 3, 1], [2, 1, 3, 2],
 [2, 1, 3, 3], [2, 2, 1, 1], [2, 2, 1, 2], [2, 2, 1, 3], [2, 2, 2, 1], 
[2, 2, 2, 2], [2, 2, 2, 3], [2, 2, 3, 1], [2, 2, 3, 2], [2, 2, 3, 3], 
[2, 3, 1, 1], [2, 3, 1, 2], [2, 3, 1, 3], [2, 3, 2, 1], [2, 3, 2, 2], 
[2, 3, 2, 3], [2, 3, 3, 1], [2, 3, 3, 2], [2, 3, 3, 3], [3, 1, 1, 1], 
[3, 1, 1, 2], [3, 1, 1, 3], [3, 1, 2, 1], [3, 1, 2, 2], [3, 1, 2, 3], 
[3, 1, 3, 1], [3, 1, 3, 2], [3, 1, 3, 3], [3, 2, 1, 1], [3, 2, 1, 2], 
[3, 2, 1, 3], [3, 2, 2, 1], [3, 2, 2, 2], [3, 2, 2, 3], [3, 2, 3, 1], 
[3, 2, 3, 2], [3, 2, 3, 3], [3, 3, 1, 1], [3, 3, 1, 2], [3, 3, 1, 3], 
[3, 3, 2, 1], [3, 3, 2, 2], [3, 3, 2, 3], [3, 3, 3, 1], [3, 3, 3, 2], 
[3, 3, 3, 3]]```

Given 3 digits a, b, and c. The task is to find all the possible combinations from these digits.

Examples:

Input: [1, 2, 3]
Output:
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1

Input: [0, 9, 5]
Output:
0 9 5
0 5 9
9 0 5
9 5 0
5 0 9
5 9 0

Method 1: Brute force or Naive approach

The naive approach is to run 3 loops from 0 to 3 and print all the numbers from the list if the indexes are not equal to each other.

Example:

Python3

def comb[L]:

    for i in range[3]:

        for j in range[3]:

            for k in range[3]:

                if [i!=j and j!=k and i!=k]:

                    print[L[i], L[j], L[k]]

comb[[1, 2, 3]]

Output:

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

Method 2: Using itertools.permutations[]

This method takes a list as an input and returns an object list of tuples that contain all permutation in a list form.

Example:

Python3

from itertools import permutations

comb = permutations[[1, 2, 3], 3]

for i in comb:

    print[i]

Output:

[1, 2, 3]
[1, 3, 2]
[2, 1, 3]
[2, 3, 1]
[3, 1, 2]
[3, 2, 1]

Python provides direct methods to find permutations and combinations of a sequence. These methods are present in itertools package.

Permutation 

First import itertools package to implement the permutations method in python. This method takes a list as an input and returns an object list of tuples that contain all permutations in a list form. 
 

Python3

from itertools import permutations 

perm = permutations[[1, 2, 3]] 

for i in list[perm]: 

    print [i] 

Output: 

[1, 2, 3]
[1, 3, 2]
[2, 1, 3]
[2, 3, 1]
[3, 1, 2]
[3, 2, 1]

It generates n! permutations if the length of the input sequence is n. 
If want  to get permutations of length L then implement it in this way. 
 

Python3

from itertools import permutations 

perm = permutations[[1, 2, 3], 2

for i in list[perm]: 

    print [i] 

Output: 

[1, 2]
[1, 3]
[2, 1]
[2, 3]
[3, 1]
[3, 2]

It generates nCr * r! permutations if the length of the input sequence is n and the input parameter is r.

Combination 

This method takes a list and an input r as an input and return an object list of tuples which contain all possible combination of length r in a list form. 
 

Python3

from itertools import combinations

comb = combinations[[1, 2, 3], 2]

for i in list[comb]:

    print [i]

Output: 

[1, 2]
[1, 3]
[2, 3]

1. Combinations are emitted in lexicographic sort order of input. So, if the input list is sorted, the combination tuples will be produced in sorted order. 
 

Python3

from itertools import combinations 

comb = combinations[[1, 2, 3], 2

for i in list[comb]: 

    print [i]

Output: 

[1, 2]
[1, 3]
[2, 3]

2. Elements are treated as unique based on their position, not on their value. So if the input elements are unique, there will be no repeat values in each combination. 
 

Python3

from itertools import combinations 

comb = combinations[[2, 1, 3], 2

for i in list[comb]: 

    print [i]

Output: 

[2, 1]
[2, 3]
[1, 3]

3. If we want to make a combination of the same element to the same element then we use combinations_with_replacement. 
 

Python3

from itertools import combinations_with_replacement 

comb = combinations_with_replacement[[1, 2, 3], 2

for i in list[comb]: 

    print [i] 

Output:

[1, 1]
[1, 2]
[1, 3]
[2, 2]
[2, 3]
[3, 3] 

How do you get all the combinations of a set of numbers in Python?

To find all the combinations of a Python list, also known as a powerset, follow these steps:.
Import the built-in itertools module..
Specify a list of items..
Initialize an empty list for storing the combinations..
Create a loop that loops values from 0 to the length of the list + 1..

How do you find all the combinations of numbers?

Remember, the formula to calculate combinations is nCr = n! / r! * [n - r]!, where n represents the number of items, and r represents the number of items being chosen at a time.

How do you find the combinations of 4 numbers in Python?

“python all possible combinations of 4 numbers” Code Answer's.
a=int[input["Enter first number:"]].
b=int[input["Enter second number:"]].
c=int[input["Enter third number:"]].
d. append[a].
d. append[b].
d. append[c].
for i in range[0,3]:.

How do you get the combination of two numbers in Python?

Combination formula : [n-r]! For example, if we have 3 elements and if we are taking 2 elements at a time, we will have 3!/2![ 3-2]! or 3 combinations.

Chủ Đề