Python – String Integer Product
Last Updated :
09 Apr, 2023
Sometimes, while working with data, we can have a problem in which we receive a series of lists with data in string format, which we wish to find the product of each string list integer. Let’s discuss certain ways in which this task can be performed.
Method #1: Using loop + int()
This is the brute force method to perform this task. In this, we run a loop for the entire list, convert each string to an integer and perform product listwise and store it in a separate list.
Python3
def prod(val) :
res = 1
for ele in val:
res * = int (ele)
return res
test_list = [[ '1' , '4' ], [ '5' , '6' ], [ '7' , '10' ]]
print ("The original list : " + str (test_list))
res = []
for sub in test_list:
par_prod = prod(sub)
res.append(par_prod)
print (" List after product of nested string lists : " + str (res))
|
Output :
The original list : [['1', '4'], ['5', '6'], ['7', '10']]
List after product of nested string lists : [4, 30, 70]
Time Complexity: O(n^2) where n is the length of the test_list
Auxiliary Space: O(n) where n is the length of the res list.
Method #2: Using loop + int() + list comprehension
This is the shorthand with the help of which this task can be performed. In this, we run a loop on lists using list comprehension and extract product using explicit product function.
Python3
def prod(val) :
res = 1
for ele in val:
res * = int (ele)
return res
test_list = [[ '1' , '4' ], [ '5' , '6' ], [ '7' , '10' ]]
print ("The original list : " + str (test_list))
res = [prod(sub) for sub in test_list]
print (" List after product of nested string lists : " + str (res))
|
Output :
The original list : [['1', '4'], ['5', '6'], ['7', '10']]
List after product of nested string lists : [4, 30, 70]
Time Complexity: O(n^2) where n is the length of the test_list
Auxiliary Space: O(n) where n is the length of the res list.
Method #3 : Using list(),map(),functors.reduce() and operator.mul
Python3
test_list = [[ '1' , '4' ], [ '5' , '6' ], [ '7' , '10' ]]
print ( "The original list : " + str (test_list))
res = []
for sub in test_list:
x = list ( map ( int ,sub))
from functools import reduce
import operator
a = reduce (operator.mul,x, 1 )
res.append(a)
print ( "List after product of nested string lists : " + str (res))
|
Output
The original list : [['1', '4'], ['5', '6'], ['7', '10']]
List after product of nested string lists : [4, 30, 70]
Time Complexity: O(n*n), where n is the length of the input list. This is because we’re using list(),map(),functors.reduce() and operator.mul 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 #4 : Using numpy.prod()
This task can be accomplished using numpy library, by using numpy.prod() method which performs product of elements of an array.
Note: Install numpy module using command “pip install numpy”
Python3
import numpy as np
test_list = [[ '1' , '4' ], [ '5' , '6' ], [ '7' , '10' ]]
print ( "The original list : " + str (test_list))
res = [np.prod([ int (y) for y in x]) for x in test_list]
print ( "List after product of nested string lists : " + str (res))
|
Output :
The original list : [['1', '4'], ['5', '6'], ['7', '10']]
List after product of nested string lists : [4, 30, 70]
Time Complexity: The time complexity is O(N) where N is the number of elements in the list.
Space Complexity: The space complexity is O(N) where N is the number of elements in the list.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...