Python | Pair Product combinations
Last Updated :
11 May, 2023
Sometimes, while working with data, we can have a problem in which we need to perform tuple multiplication among all the tuples in list. This can have application in many domains. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using combinations() + list comprehension This problem can be solved using combinations of above functions. In this, we use combinations() to generate all possible combination among tuples and list comprehension is used to feed product logic.
Python3
from itertools import combinations
test_list = [( 2 , 4 ), ( 6 , 7 ), ( 5 , 1 ), ( 6 , 10 )]
print ( "The original list : " + str (test_list))
res = [(b1 * a1, b2 * a2) for (a1, a2), (b1, b2) in combinations(test_list, 2 )]
print ( "The Product pair combinations are : " + str (res))
|
Output :
The original list : [(2, 4), (6, 7), (5, 1), (6, 10)]
The Product pair combinations are : [(12, 28), (10, 4), (12, 40), (30, 7), (36, 70), (30, 10)]
Time complexity: O(M^N) as the number of combinations generated is M choose N.
Auxiliary space: O(M^N) as the size of the resultant list is also M choose N.
Method #2 : Using list comprehension + zip() + operator.mul + combinations() The combinations of above methods can also solve this problem. In this, we perform the task of product using mul() and the like indexed elements are linked using zip().
Python3
from itertools import combinations
import operator
test_list = [( 2 , 4 ), ( 6 , 7 ), ( 5 , 1 ), ( 6 , 10 )]
print ( "The original list : " + str (test_list))
res = [(operator.mul( * a), operator.mul( * b))\
for a, b in ( zip (y, x) for x, y in combinations(test_list, 2 ))]
print ( "The Product pair combinations are : " + str (res))
|
Output :
The original list : [(2, 4), (6, 7), (5, 1), (6, 10)]
The Product pair combinations are : [(12, 28), (10, 4), (12, 40), (30, 7), (36, 70), (30, 10)]
Time complexity: O(M^N) as the number of combinations generated is M choose N.
Auxiliary space: O(M^N) as the size of the resultant list is also M choose N.
Method #3 : Using reduce and lambda
This problem can be solved using combinations of above functions. In this, The reduce() function applies a lambda function that multiplies the first and second elements of each tuple cumulatively to get the final result. The output is a list of tuples containing the product of each possible combination of tuples in test_list.
Python3
from functools import reduce
from itertools import combinations
test_list = [( 2 , 4 ), ( 6 , 7 ), ( 5 , 1 ), ( 6 , 10 )]
print ( "The original list : " + str (test_list))
res = reduce ( lambda a, b: a + [(b[ 0 ][ 0 ] * b[ 1 ][ 0 ], b[ 0 ][ 1 ] * b[ 1 ][ 1 ])], combinations(test_list, 2 ), [])
print ( "The Product pair combinations are : " + str (res))
|
Output
The original list : [(2, 4), (6, 7), (5, 1), (6, 10)]
The Product pair combinations are : [(12, 28), (10, 4), (12, 40), (30, 7), (36, 70), (30, 10)]
Time complexity: O(N^N), because the combinations() function generates all possible pairs of tuples and the reduce() function applies the lambda function to each pair.
Auxiliary space: O(N), where N is the length of the input list test_list
Share your thoughts in the comments
Please Login to comment...