Python – Absolute Tuple Summation
Last Updated :
05 May, 2023
Sometimes, while working with Python tuples, we can have a problem in which we need to perform the summation of absolute values of intermediate tuple elements. This kind of problem can have application in many domains such as web development. Let’s discuss certain ways in which this task can be performed.
Input : test_list = [(-7, -8), (-5, -6)] Output : [15, 11] Input : test_list = [(-1, -2, -4)] Output : [7]
Method #1 : Using sum() + list comprehension + abs() The combination of above functions can be used to solve this problem. In this, we perform the task of computing sum using sum(), abs() for absolute values, and list comprehension to iterate through the list and inside each tuple, generator expression is used to iterate.
Python3
test_list = [( 7 , - 8 ), ( - 5 , - 6 ), ( - 7 , 2 ), ( 6 , 8 )]
print ("The original list is : " + str (test_list))
res = [ sum ([ abs (ele) for ele in sub]) for sub in test_list]
print ("The absolute sum list : " + str (res))
|
Output :
The original list is : [(7, -8), (-5, -6), (-7, 2), (6, 8)]
The absolute sum list: [15, 11, 9, 14]
Method #2 : Using list comprehension + sum() + map() The combination of above functions offer an alternative to solve this problem. In this, we perform the task of computing sum of entire tuple elements using map() rather than generator expression.
Python3
test_list = [( 7 , - 8 ), ( - 5 , - 6 ), ( - 7 , 2 ), ( 6 , 8 )]
print ("The original list is : " + str (test_list))
res = [ sum ( map ( abs , ele)) for ele in test_list]
print ("The absolute sum list : " + str (res))
|
Output :
The original list is : [(7, -8), (-5, -6), (-7, 2), (6, 8)]
The absolute sum list: [15, 11, 9, 14]
Method #3 : Using recursion+abs()
In the above example, the given list is test_list = [(-7, -8), (-5, -6)]. In the first iteration of the loop, the absolute values of -7 and -8 are taken and stored in the abs_tup variable as (7, 8). The sum of these absolute values is 15, which is appended to the result list. In the second iteration, the absolute values of -5 and -6 are taken and stored in the abs_tup variable as (5, 6). The sum of these absolute values is 11, which is appended to the result list. Finally, the result list is returned as the output.
Python3
def absolute_tuple_summation(test_list):
result = []
for tup in test_list:
abs_tup = tuple ( abs (num) for num in tup)
abs_sum = sum (abs_tup)
result.append(abs_sum)
return result
test_list = [( - 7 , - 8 ), ( - 5 , - 6 )]
result = absolute_tuple_summation(test_list)
print (result)
|
Time complexity: O(n*m)
Auxiliary Space: O(n)
Method #4 : Using nested for loops
Approach
- Initiated for loop to traverse list of tuples
- Initiate another for loop inside above loop to traverse each tuple
- With in for loop if tuple element is less than zero multiply with -1 and find the sum of elements of tuple
- Add the sum to output list
- Display output list
Python3
test_list = [( 7 , - 8 ), ( - 5 , - 6 ), ( - 7 , 2 ), ( 6 , 8 )]
print ( "The original list is : " + str (test_list))
res = []
for i in test_list:
s = 0
for j in i:
if (j< 0 ):
s + = ( - 1 ) * j
else :
s + = j
res.append(s)
print ( "The absolute sum list: " + str (res))
|
Output
The original list is : [(7, -8), (-5, -6), (-7, 2), (6, 8)]
The absolute sum list: [15, 11, 9, 14]
Time Complexity : O(n*m) n – length of tuples list m- length of each tuple
Auxiliary Space : O(n)
Method #5 : Using numpy:
Algorithm:
- Initialize the test_list with tuples.
- Use np.abs() function to calculate the absolute values of the tuples in the test_list.
- Use np.sum() function to calculate the sum of absolute values of each tuple across the rows (axis=1) of the array.
- Assign the result to the variable ‘res’.
- Print the original list an
Python3
import numpy as np
test_list = [( 7 , - 8 ), ( - 5 , - 6 ), ( - 7 , 2 ), ( 6 , 8 )]
res = np. abs (test_list). sum (axis = 1 )
print ( "The original list is : " + str (test_list))
print ( "The absolute sum list: " + str (res))
|
Output:
The original list is : [(7, -8), (-5, -6), (-7, 2), (6, 8)]
The absolute sum list: [15 11 9 14]
Time Complexity:
The time complexity of this code is O(n), where n is the length of the test_list. The np.abs() and np.sum() functions have a complexity of O(n).
Auxiliary Space:
The space complexity of this code is O(n), where n is the length of the test_list. This is due to the creation of the numpy array ‘res’ which stores the absolute sum of each tuple.
Method #6 : Using lambda function and map()
- Initialize the list of tuples.
- Define a lambda function that takes a tuple as input and returns the absolute sum of its elements.
- Use the map() function to apply the lambda function to each tuple in the list, and store the results in a new list.
- Print the original list and the resulting list of absolute sums.
Python3
test_list = [( 7 , - 8 ), ( - 5 , - 6 ), ( - 7 , 2 ), ( 6 , 8 )]
print ( "The original list is : " + str (test_list))
abs_sum = list ( map ( lambda tup: sum ( map ( abs , tup)), test_list))
print ( "The absolute sum list: " + str (abs_sum))
|
Output
The original list is : [(7, -8), (-5, -6), (-7, 2), (6, 8)]
The absolute sum list: [15, 11, 9, 14]
Time complexity: O(n)
Auxiliary space: O(n)
Share your thoughts in the comments
Please Login to comment...