Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python program to sort tuples by frequency of their absolute difference
When it is required to sort tuples by frequency of their absolute difference, the lambda function, the abs method and the sorted method are used. This technique sorts tuples based on how often their absolute difference appears in the dataset.
Understanding Absolute Difference
The absolute difference between two numbers is the positive difference between them. For a tuple (a, b), the absolute difference is abs(a - b).
Example
Below is a demonstration of sorting tuples by frequency of their absolute difference ?
my_list = [(11, 26), (21, 33), (90, 11), (26, 21), (32, 18), (25, 37)]
print("The list is :")
print(my_list)
my_diff_list = [abs(x - y) for x, y in my_list]
my_result = sorted(my_list, key = lambda sub: my_diff_list.count(abs(sub[0] - sub[1])))
print("The resultant list is :")
print(my_result)
The list is : [(11, 26), (21, 33), (90, 11), (26, 21), (32, 18), (25, 37)] The resultant list is : [(11, 26), (90, 11), (26, 21), (32, 18), (21, 33), (25, 37)]
How It Works
Let's break down the absolute differences to understand the sorting ?
my_list = [(11, 26), (21, 33), (90, 11), (26, 21), (32, 18), (25, 37)]
# Calculate all absolute differences
differences = []
for x, y in my_list:
diff = abs(x - y)
differences.append(diff)
print(f"Tuple {(x, y)}: abs({x} - {y}) = {diff}")
print("\nDifferences list:", differences)
# Count frequency of each difference
from collections import Counter
freq_count = Counter(differences)
print("Frequency count:", dict(freq_count))
Tuple (11, 26): abs(11 - 26) = 15
Tuple (21, 33): abs(21 - 33) = 12
Tuple (90, 11): abs(90 - 11) = 79
Tuple (26, 21): abs(26 - 21) = 5
Tuple (32, 18): abs(32 - 18) = 14
Tuple (25, 37): abs(25 - 37) = 12
Differences list: [15, 12, 79, 5, 14, 12]
Frequency count: {15: 1, 12: 2, 79: 1, 5: 1, 14: 1}
Alternative Approach Using Counter
A more efficient approach using Counter to avoid repeated calculations ?
from collections import Counter
my_list = [(11, 26), (21, 33), (90, 11), (26, 21), (32, 18), (25, 37)]
# Calculate differences and their frequencies
differences = [abs(x - y) for x, y in my_list]
freq_counter = Counter(differences)
# Sort by frequency (ascending order)
result = sorted(my_list, key=lambda sub: freq_counter[abs(sub[0] - sub[1])])
print("Original list:")
print(my_list)
print("\nSorted by frequency of absolute difference:")
print(result)
Original list: [(11, 26), (21, 33), (90, 11), (26, 21), (32, 18), (25, 37)] Sorted by frequency of absolute difference: [(11, 26), (90, 11), (26, 21), (32, 18), (21, 33), (25, 37)]
Key Points
- Tuples with less frequent absolute differences appear first
- The
lambdafunction calculates the frequency count for each tuple - Using
Counteris more efficient for larger datasets - The sorting is stable, maintaining relative order for equal frequencies
Conclusion
This technique sorts tuples by how frequently their absolute difference occurs in the dataset. Tuples with rarer differences are placed first, making it useful for identifying unique patterns in paired data.
