Python program to sort tuples by frequency of their absolute difference
Last Updated :
15 May, 2023
Given a list of dual tuples, the task here is to write a Python program that can sort them by the frequency of their elements’ absolute differences.
Input : [(1, 6), (11, 3), (9, 1), (6, 11), (2, 10), (5, 7)]
Output : [(5, 7), (1, 6), (6, 11), (11, 3), (9, 1), (2, 10)]
Explanation : 7 - 5 = 2 occurs only 1 time. 5 occurs twice [( 6 - 1), (11 - 6)]
and 8 occurs 3 times as difference.
Input : [(1, 6), (6, 11), (5, 7)]
Output : [(5, 7), (1, 6), (6, 11)]
Explanation : 7 - 5 = 2 occurs only 1 time. 5 occurs twice [( 6 - 1), (11 - 6)].
Method 1: Using sorted(), abs(), count() and list comprehension
In this, we perform the task of computing each absolute difference using abs() and list comprehension. Then, sorted() and count() are used to sort tuples based on computed results of absolute difference.
Python3
test_list = [( 1 , 6 ), ( 11 , 3 ), ( 9 , 1 ), ( 6 , 11 ), ( 2 , 10 ), ( 5 , 7 )]
print ( "The original list is : " + str (test_list))
diff_list = [ abs (x - y) for x, y in test_list]
res = sorted (test_list, key = lambda sub: diff_list.count( abs (sub[ 0 ] - sub[ 1 ])))
print ( "Sorted Tuples : " + str (res))
|
Output:
The original list is : [(1, 6), (11, 3), (9, 1), (6, 11), (2, 10), (5, 7)]
Sorted Tuples : [(5, 7), (1, 6), (6, 11), (11, 3), (9, 1), (2, 10)]
Time Complexity: O(n*logn)
Auxiliary Space: O(1)
Method 2: operator.countOf() and list comprehension
Python3
import operator as op
test_list = [( 1 , 6 ), ( 11 , 3 ), ( 9 , 1 ), ( 6 , 11 ), ( 2 , 10 ), ( 5 , 7 )]
print ( "The original list is : " + str (test_list))
diff_list = [ abs (x - y) for x, y in test_list]
res = sorted (test_list, key = lambda sub: op.countOf(diff_list, abs (sub[ 0 ] - sub[ 1 ])))
print ( "Sorted Tuples : " + str (res))
|
Output
The original list is : [(1, 6), (11, 3), (9, 1), (6, 11), (2, 10), (5, 7)]
Sorted Tuples : [(5, 7), (1, 6), (6, 11), (11, 3), (9, 1), (2, 10)]
Time Complexity: O(NLogN), where n is the length of the given string
Auxiliary Space: O(N)
Method 3: Using the map() function and a lambda function
Step-by-step approach:
- Initialize the list of tuples.
- Use the map() function to create a list of the absolute differences between each tuple’s elements:
- Use the sorted() function with a lambda function as the key argument to sort the list of tuples by their corresponding absolute differences.
- Print the sorted list of tuples.
Python3
test_list = [( 1 , 6 ), ( 11 , 3 ), ( 9 , 1 ), ( 6 , 11 ), ( 2 , 10 ), ( 5 , 7 )]
print ( "The original list is : " + str (test_list))
diff_list = list ( map ( lambda t: abs (t[ 0 ] - t[ 1 ]), test_list))
res = sorted (test_list, key = lambda t: diff_list[test_list.index(t)])
print ( "Sorted Tuples : " + str (res))
|
Output
The original list is : [(1, 6), (11, 3), (9, 1), (6, 11), (2, 10), (5, 7)]
Sorted Tuples : [(5, 7), (1, 6), (6, 11), (11, 3), (9, 1), (2, 10)]
Time complexity: O(nlogn), where n is the length of the list of tuples since sorting takes O(nlogn) time.
Auxiliary space: O(n), since we create a list of absolute differences that is the same length as the list of tuples
Method 4: Using the heapq module
- Compute the absolute differences between the first and second elements of each tuple in the test_list using a list comprehension, and store the resulting list in diff_list.
- Create a new list of tuples named heap by zipping together diff_list and test_list using the zip() function, creating a tuple of (diff, t) for each element t in test_list and its corresponding absolute difference.
- Use the heapify() function from the heapq module to transform heap into a heap data structure. This step ensures that the tuples in heap are in heap order, which means that the smallest absolute difference is at the root of the heap.
- Use a list comprehension to extract the second element of each tuple in heap in heap order by repeatedly calling the heappop() function from the heapq module, which returns and removes the smallest element in the heap. We repeat this process a number of times equal to the length of the heap, storing each second element of the popped tuple in a new list named res.
- Print the sorted list of tuples named res using the print() function and string formatting to convert the list to a string.
Python3
import heapq
test_list = [( 1 , 6 ), ( 11 , 3 ), ( 9 , 1 ), ( 6 , 11 ), ( 2 , 10 ), ( 5 , 7 )]
print ( "The original list is : " + str (test_list))
diff_list = [ abs (t[ 0 ] - t[ 1 ]) for t in test_list]
heap = [(diff, t) for diff, t in zip (diff_list, test_list)]
heapq.heapify(heap)
res = [heapq.heappop(heap)[ 1 ] for _ in range ( len (heap))]
print ( "Sorted Tuples : " + str (res))
|
Output
The original list is : [(1, 6), (11, 3), (9, 1), (6, 11), (2, 10), (5, 7)]
Sorted Tuples : [(5, 7), (1, 6), (6, 11), (2, 10), (9, 1), (11, 3)]
Time complexity: O(n log n)
Auxiliary space: O(n)
Similar Reads
Python program to Sort Tuples by their Maximum element
Given a Tuple List sort tuples by maximum element in a tuple. Input : test_list = [(4, 5, 5, 7), (1, 3, 7, 4), (19, 4, 5, 3), (1, 2)] Output : [(19, 4, 5, 3), (4, 5, 5, 7), (1, 3, 7, 4), (1, 2)] Explanation : 19 > 7 = 7 > 2, is order, hence reverse sorted by maximum element. Input : test_list
5 min read
Python program to Sort a List of Dictionaries by the Sum of their Values
Given Dictionary List, sort by summation of their values. Input : test_list = [{1 : 3, 4 : 5, 3 : 5}, {1 : 100}, {8 : 9, 7 : 3}] Output : [{8: 9, 7: 3}, {1: 3, 4: 5, 3: 5}, {1: 100}] Explanation : 12 < 13 < 100, sorted by values sum Input : test_list = [{1 : 100}, {8 : 9, 7 : 3}] Output : [{8:
6 min read
Python program to find sum of absolute difference between all pairs in a list
Given a list of distinct elements, write a Python program to find the sum of absolute differences of all pairs in the given list.Examples: Input : [9, 2, 14] Output : 24 Explanation: (abs(9-2) + abs(9-14) + abs(2-14)) Input : [1, 2, 3, 4] Output : 10 Explanation: (abs(1-2) + abs(1-3) + abs(1-4) + ab
5 min read
Python Program to Sort Matrix Rows by summation of consecutive difference of elements
Given a Matrix, the following article depicts how to sort rows of a matrix on the basis of summation of difference between consecutive elements of a row. Input : test_list = [[1, 5, 3, 6], [4, 3, 2, 1], [7, 2, 4, 5], [6, 9, 3, 2]], Output : [[4, 3, 2, 1], [7, 2, 4, 5], [1, 5, 3, 6], [6, 9, 3, 2]] Ex
7 min read
Python | Sort tuple list on basis of difference of elements
Sometimes, while working with data, we can have a problem of sorting them. There are many types of basis on which sorting can be performed. But this article discusses sorting on basis of difference of both elements of pair. Let's discuss certain ways in which this can be done. Method #1 : Using sort
5 min read
Python program to sort a list of tuples alphabetically
Given a list of tuples, write a Python program to sort the tuples alphabetically by the first item of each tuple. Examples: Input: [("Amana", 28), ("Zenat", 30), ("Abhishek", 29), ("Nikhil", 21), ("B", "C")] Output: [('Amana', 28), ('Abhishek', 29), ('B', 'C'), ('Nikhil', 21), ('Zenat', 30)] Input:
3 min read
Python Program for Maximum difference between groups of size two
Given an array of even number of elements, form groups of 2 using these array elements such that the difference between the group with highest sum and the one with lowest sum is maximum.Note: An element can be a part of one group only and it has to be a part of at least 1 group. Examples: Input : ar
3 min read
Python Program to Remove duplicate tuples irrespective of order
Given a list of binary tuples, the task is to write a Python program to remove all tuples that are duplicates irrespective of order, i.e delete if contains similar elements, irrespective of order. Input : test_list = [(4, 6), (1, 2), (9, 2), (2, 1), (5, 7), (6, 4), (9, 2)]Output : [(1, 2), (5, 7), (
6 min read
Python Program to get all possible differences between set elements
Given a set, the task is to write a Python program to get all possible differences between its elements. Input : test_set = {1, 5, 2, 7, 3, 4, 10, 14} Output : {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13} Explanation : All possible differences are computed. Input : test_set = {1, 5, 2, 7} Output : {1
4 min read
Python - Sort by Frequency of second element in Tuple List
Given list of tuples, sort by frequency of second element of tuple. Input : test_list = [(6, 5), (1, 7), (2, 5), (8, 7), (9, 8), (3, 7)] Output : [(1, 7), (8, 7), (3, 7), (6, 5), (2, 5), (9, 8)] Explanation : 7 occurs 3 times as 2nd element, hence all tuples with 7, are aligned first. Input : test_l
6 min read