Python – Cumulative Records Product
Last Updated :
17 Apr, 2023
Sometimes, while working with data in form of records, we can have a problem in which we need to find the product element of all the records received. This is a very common application that can occur in Data Science domain. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using loop + generator expression
This is the most basic method to achieve solution to this task. In this, we iterate over whole nested lists using generator expression and get the product element using explicit product function.
Python3
def prod(val) :
res = 1
for ele in val:
res * = ele
return res
test_list = [( 2 , 4 ), ( 6 , 7 ), ( 5 , 1 ), ( 6 , 10 ), ( 8 , 7 )]
print ( "The original list : " + str (test_list))
res = prod( int (j) for i in test_list for j in i)
print ( "The Cumulative product of list is : " + str (res))
|
Output
The original list : [(2, 4), (6, 7), (5, 1), (6, 10), (8, 7)]
The Cumulative product of list is : 5644800
Time complexity: O(nm), where n is the number of sublists and m is the length of each sublist.
Auxiliary space: O(1), as we are only using a few variables to store the intermediate results.
Method #2 : Using loop + map() + chain.from_iterable()
The combination of above methods can also be used to perform this task. In this, the extension of finding product is done by combination of map() and from_iterable().
Python3
from itertools import chain
def prod(val) :
res = 1
for ele in val:
res * = ele
return res
test_list = [( 2 , 4 ), ( 6 , 7 ), ( 5 , 1 ), ( 6 , 10 ), ( 8 , 7 )]
print ( "The original list : " + str (test_list))
res = prod( map ( int , chain.from_iterable(test_list)))
print ( "The cumulative product of list is : " + str (res))
|
Output
The original list : [(2, 4), (6, 7), (5, 1), (6, 10), (8, 7)]
The cumulative product of list is : 5644800
The time complexity of this program is O(nk), where n is the number of tuples in the input list and k is the maximum number of elements in a tuple.
The auxiliary space complexity of this program is O(1), as it only uses a constant amount of memory to store the result and temporary variables.
Method #3: Using functools.reduce() and operator.mul
This approach uses the reduce function from the functools library along with the mul operator from the operator library to perform the product of all elements in the records. This method can handle any level of nesting.
Python3
from functools import reduce
import operator
test_list = [( 2 , 4 ), ( 6 , 7 ), ( 5 , 1 ), ( 6 , 10 ), ( 8 , 7 )]
print ( "The original list : " + str (test_list))
res = reduce (operator.mul, [j for i in test_list for j in i])
print ( "The cumulative product of list is : " + str (res))
|
Output
The original list : [(2, 4), (6, 7), (5, 1), (6, 10), (8, 7)]
The cumulative product of list is : 5644800
Time complexity of the above method is O(n) and an auxiliary space is O(n) where n is the number of elements in the records.
Method #3: Using a for loop:
Python3
test_list = [( 2 , 4 ), ( 6 , 7 ), ( 5 , 1 ), ( 6 , 10 ), ( 8 , 7 )]
res = 1
print ( "The original list : " + str (test_list))
for i in test_list:
for j in i:
res * = j
print ( "The cumulative product of list is : " + str (res))
|
Output
The original list : [(2, 4), (6, 7), (5, 1), (6, 10), (8, 7)]
The cumulative product of list is : 5644800
Time Complexity: O(N)
Auxiliary Space: O(1)
Method #4: Using numpy.cumprod()
Another method to calculate the cumulative product of a list is by using the cumprod() function provided by the numpy library. This method can be faster and more efficient than the previous methods, especially for larger lists.
Here’s how to implement it step by step:
- Import the numpy library.
- Initialize the list.
- Convert the list to a numpy array using np.array().
- Use np.cumprod() to calculate the cumulative product of the array.
- Access the last element of the resulting array to get the cumulative product of the original list.
Python3
import numpy as np
test_list = [( 2 , 4 ), ( 6 , 7 ), ( 5 , 1 ), ( 6 , 10 ), ( 8 , 7 )]
test_array = np.array(test_list)
cumulative_product = np.cumprod(test_array)
result = cumulative_product[ - 1 ]
print ( "The cumulative product of the list is:" , result)
|
OUTPUT:
The cumulative product of the list is: 5644800
Time complexity: O(n), where n is the number of elements in the list.
Auxiliary space: O(n), as the numpy array of the same size as the original list is created.
Share your thoughts in the comments
Please Login to comment...