How do you find a combination in python?

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] 

Given an array of size n, generate and print all possible combinations of r elements in array.

Examples:

Input : arr[] = [1, 2, 3, 4],  
            r = 2
Output : [[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]]

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

This problem has existing recursive solution please refer Print all possible combinations of r elements in a given array of size n link. We will solve this problem in python using itertools.combinations[] module.

What does itertools.combinations[] do ?

It returns r length subsequences of elements from the input iterable. Combinations are emitted in lexicographic sort order. So, if the input iterable is sorted, the combination tuples will be produced in sorted order.

  • itertools.combinations[iterable, r] :
    It return r-length tuples in sorted order with no repeated elements. For Example, combinations[‘ABCD’, 2] ==> [AB, AC, AD, BC, BD, CD].
  • itertools.combinations_with_replacement[iterable, r] :
    It return r-length tuples in sorted order with repeated elements. For Example, combinations_with_replacement[‘ABCD’, 2] ==> [AA, AB, AC, AD, BB, BC, BD, CC, CD, DD].
  • from itertools import combinations

    def rSubset[arr, r]:

        return list[combinations[arr, r]]

    if __name__ == "__main__":

        arr = [1, 2, 3, 4]

        r = 2

        print [rSubset[arr, r]]

    Output:

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

    This article is contributed by Shashank Mishra [Gullu]. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

How do you calculate combinations in Python?

To calculate the combinations of a tuple in Python, use the itertools. combinations[] method. The combinations[] method takes a tuple as an argument and returns all the possible combinations of elements of the tuple. Let's define a tuple and perform the combinations on the item of the tuple.

How do you find the combination of a string in Python?

To find all possible permutations of a given string, you can use the itertools module which has a useful method called permutations[iterable[, r]]. This method return successive r length permutations of elements in the iterable as tuples.

How do you find the combination of a two list in Python?

How to get all unique combinations of two lists in Python.
list1 = ["a", "b", "c"].
list2 = [1, 2].
all_combinations = [].
list1_permutations = itertools. permutations[list1, len[list2]] ... .
for each_permutation in list1_permutations:.
zipped = zip[each_permutation, list2].
all_combinations. ... .
print[all_combinations].

Chủ Đề