Python – Consecutive Tuple difference
Last Updated :
23 Apr, 2023
Given List of tuples, find index-wise absolute difference of consecutive tuples.
Input : test_list = [(5, 3), (1, 4), (9, 5), (3, 5)] Output : [(4, 1), (7, 1), (6, 0)] Explanation : 5 – 1 = 4, 4 – 3 = 1. hence (4, 1) and so on. Input : test_list = [(9, 5), (3, 5)] Output : [(6, 0)] Explanation : 9 – 3 = 6, 5 – 5 = 0.
Method #1 : Using list comprehension
This is one way in which this task can be performed. In this, we employ brute force strategy on list using list comprehension strategy to perform this task as a one-liner.
Python3
test_list = [( 6 , 3 ), ( 1 , 4 ), ( 8 , 5 ), ( 3 , 5 )]
print ( "The original list : " + str (test_list))
res = [( abs (test_list[idx + 1 ][ 0 ] - test_list[idx][ 0 ]), abs (test_list[idx + 1 ][ 1 ] - test_list[idx][ 1 ]))
for idx in range ( len (test_list) - 1 )]
print ( "The extracted list : " + str (res))
|
Output
The original list : [(6, 3), (1, 4), (8, 5), (3, 5)]
The extracted list : [(5, 1), (7, 1), (5, 0)]
Time Complexity: O(n), where n is the elements in the list
Auxiliary Space: O(n), where n is the size of list
Method #2 : Using tuple() + map() + sub
The combination of above functions can be used to solve this problem. In this, we perform the task of perform subtraction using sub and map() is used to extend the logic to whole list elements. Doesn’t output absolute result.
Python3
from operator import sub
test_list = [( 6 , 3 ), ( 1 , 4 ), ( 8 , 5 ), ( 3 , 5 )]
print ( "The original list : " + str (test_list))
res = []
for idx in range ( len (test_list) - 1 ):
res.append( tuple ( map (sub, test_list[idx + 1 ][ 0 :], test_list[idx][ 0 :])))
print ( "The extracted list : " + str (res))
|
Output
The original list : [(6, 3), (1, 4), (8, 5), (3, 5)]
The extracted list : [(-5, 1), (7, 1), (-5, 0)]
Time Complexity: O(n) where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n) where n is the number of elements in the list “test_list”.
Method 3 : Using numpy library
Step by step approach :
- The numpy module is imported with the alias “np”.
- A list named “test_list” is initialized with four tuples as its elements.
- The original list is printed using the “print()” function with a string concatenation of “The original list : ” and the list converted to a string.
- The list “test_list” is converted to a numpy array named “arr” using the “np.array()” function.
- A new numpy array named “diff_arr” is created by subtracting the first n-1 elements of “arr” from the last n-1 elements of “arr”, where n is the length of the array.
- The numpy array “diff_arr” is converted back to a list of tuples named “res” using a list comprehension and the “tuple()” function.
- The extracted list is printed using the “print()” function with a string concatenation of “The extracted list : ” and the list converted to a string.
Python3
import numpy as np
test_list = [( 6 , 3 ), ( 1 , 4 ), ( 8 , 5 ), ( 3 , 5 )]
print ( "The original list : " + str (test_list))
arr = np.array(test_list)
diff_arr = arr[ 1 :] - arr[: - 1 ]
res = [ tuple (row) for row in diff_arr]
print ( "The extracted list : " + str (res))
|
OUTPUT :
The original list : [(6, 3), (1, 4), (8, 5), (3, 5)]
The extracted list : [(-5, 1), (7, 1), (-5, 0)]
Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(n), where n is the length of the input list (used to store the numpy array).
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...