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
Selected Reading
Python – Sort by range inclusion
Sometimes we need to sort a list of sublists based on how many tuples fall within a specific range. This technique uses list comprehension with abs() and sum() to calculate range inclusion scores for sorting.
Syntax
def range_inclusion_score(sublist, min_val, max_val):
return sum([abs(tuple[1] - tuple[0]) for tuple in sublist
if min_val < tuple[0] < max_val and min_val < tuple[1] < max_val])
my_list.sort(key=lambda x: range_inclusion_score(x, i, j))
Example
Here's how to sort sublists based on the sum of absolute differences of tuples within a range ?
def sum_range_incl(my_row):
i, j = 2, 15 # Define range bounds inside function for clarity
return sum([abs(element[1] - element[0]) for element in my_row
if element[0] > i and element[0] < j and element[1] > i and element[1] < j])
my_list = [[(12, 4), (55, 10), (11, 16)], [(42, 14)], [(2, 5), (2, 28), (9, 16)], [(12, 6), (6, 13)]]
print("The list is:")
print(my_list)
my_list.sort(key=sum_range_incl)
print("The resultant list is:")
print(my_list)
The list is: [[(12, 4), (55, 10), (11, 16)], [(42, 14)], [(2, 5), (2, 28), (9, 16)], [(12, 6), (6, 13)]] The resultant list is: [[(42, 14)], [(2, 5), (2, 28), (9, 16)], [(12, 4), (55, 10), (11, 16)], [(12, 6), (6, 13)]]
How It Works
The function calculates a score for each sublist by ?
- Filtering tuples where both values fall within the range (2, 15)
- Computing
abs(second - first)for qualifying tuples - Summing these absolute differences
- Sorting sublists by their total scores in ascending order
Better Implementation with Parameters
Here's a more flexible version that accepts range parameters ?
def calculate_range_score(sublist, min_range, max_range):
"""Calculate sum of absolute differences for tuples within range"""
score = 0
for element in sublist:
if min_range < element[0] < max_range and min_range < element[1] < max_range:
score += abs(element[1] - element[0])
return score
data = [[(12, 4), (55, 10), (11, 16)], [(42, 14)], [(2, 5), (2, 28), (9, 16)], [(12, 6), (6, 13)]]
print("Original data:")
for i, sublist in enumerate(data):
score = calculate_range_score(sublist, 2, 15)
print(f"Sublist {i}: {sublist} ? Score: {score}")
# Sort using lambda with parameters
data.sort(key=lambda x: calculate_range_score(x, 2, 15))
print("\nSorted by range inclusion score:")
print(data)
Original data: Sublist 0: [(12, 4), (55, 10), (11, 16)] ? Score: 8 Sublist 1: [(42, 14)] ? Score: 0 Sublist 2: [(2, 5), (2, 28), (9, 16)] ? Score: 7 Sublist 3: [(12, 6), (6, 13)] ? Score: 13 Sorted by range inclusion score: [[(42, 14)], [(2, 5), (2, 28), (9, 16)], [(12, 4), (55, 10), (11, 16)], [(12, 6), (6, 13)]]
Conclusion
Range inclusion sorting calculates scores based on tuples falling within specified bounds. Use sort() with a custom key function that filters by range and sums absolute differences for effective data organization.
Advertisements
