Sometimes, while working with data, we can have a problem in which we need to find accumulative product of each index in tuples. This problem can have application in web development and competitive programming domain. Let’s discuss certain way in which this problem can be solved.
Method 1: Using accumulate() + loop + lambda + map() + tuple() + zip() The combination of above functions can be used to solve this task. In this, we pair the elements using zip(), the then we perform the product of them and we extend this to all elements using map(). The taking forward of product is done by using accumulate. The binding of all logic is done by lambda function.
Python3
# Python3 code to demonstrate working of # Incremental Records Product # Using accumulate() + loop + lambda + map() + tuple() + zip() from itertools import accumulate def prod(val) : res = 1 for ele in val: res * = ele return res # initialize list test_list = [( 3 , 4 , 5 ), ( 4 , 5 , 7 ), ( 1 , 4 , 10 )] # printing original list print ("The original list : " + str (test_list)) # Incremental Records Product # Using accumulate() + loop + lambda + map() + tuple() + zip() res = list (accumulate(test_list, lambda i, j: tuple ( map (prod, zip (i, j))))) # printing result print ("Accumulative index product of tuple list : " + str (res)) |
The original list : [(3, 4, 5), (4, 5, 7), (1, 4, 10)] Accumulative index product of tuple list : [(3, 4, 5), (12, 20, 35), (12, 80, 350)]
Time Complexity: O(n*n), where n is the length of the input list. This is because we’re using accumulate() + loop + lambda + map() + tuple() + zip() which has a time complexity of O(n*n) in the worst case.
Auxiliary Space: O(n), as we’re using additional space res other than the input list itself with the same size of input list
Method 2: Using numpy.cumprod()
Note: Install numpy module using command “pip install numpy”
We can use the numpy library’s cumprod() function to find the cumulative product of each element in an array. This method is more efficient than the previous one as it uses the built-in numpy functions which are optimized for numerical calculations.
Python3
import numpy as np # initialize list test_list = [( 3 , 4 , 5 ), ( 4 , 5 , 7 ), ( 1 , 4 , 10 )] # printing original list print ( "The original list : " + str (test_list)) # Incremental Records Product using numpy.cumprod() res = np.cumprod(test_list, axis = 0 ) # printing result print ( "Accumulative index product of tuple list : " + str ([ tuple (i) for i in res])) #This code is contributed by Edula Vinay Kumar Reddy |
Output:
The original list : [(3, 4, 5), (4, 5, 7), (1, 4, 10)] Accumulative index product of tuple list : [(3, 4, 5), (12, 20, 35), (12, 80, 350)]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 3: Using itertools.accumulate() and operator.mul
- Import itertools.accumulate() and operator.mul.
- Initialize the input list of tuples.
- Use itertools.accumulate() to generate a new list of tuples where each tuple contains the cumulative products of the elements in the corresponding tuple of the input list.
- For each tuple in the input list, use map() and operator.mul to compute the element-wise product of that tuple with the corresponding tuple in the new list generated in step 3.
- Collect the resulting tuples in a list and return it.
Python3
# Python3 code to demonstrate working of # Incremental Records Product # Using itertools.accumulate() and operator.mul from itertools import accumulate from operator import mul # initialize list test_list = [( 3 , 4 , 5 ), ( 4 , 5 , 7 ), ( 1 , 4 , 10 )] # printing original list print ( "The original list : " + str (test_list)) # Incremental Records Product res = list (accumulate(test_list, lambda x, y: tuple ( map (mul, x, y)))) # printing result print ( "Accumulative index product of tuple list : " + str (res)) |
The original list : [(3, 4, 5), (4, 5, 7), (1, 4, 10)] Accumulative index product of tuple list : [(3, 4, 5), (12, 20, 35), (12, 80, 350)]
Time Complexity: O(n*m), where n is the number of tuples in the input list and m is the number of elements in each tuple. This is because the program must iterate over each element in each tuple.
Auxiliary Space: O(n*m), where n is the number of tuples in the input list and m is the number of elements in each tuple. This is because the program stores the cumulative products of each element in each tuple in a separate list.