Open In App

Python | Mathematical Median of Cumulative Records

Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, while working with Python tuple list, we can have a problem in which we need to find the median of tuple values in the list. This problem has the possible application in many domains including mathematics. Let’s discuss certain ways in which this task can be performed. 

Method #1 : Using loops + “~” operator This task can be performed in brute force manner using the combination of above functionalities. In this, we sort the list and the by using the property of “~” operator to perform negation, we access the list from front and rear after flattening, performing the required computation required for finding median. 

Python3




# Python3 code to demonstrate working of
# Mathematical Median of Cumulative Records
# Using loop + "~" operator
 
# initializing list
test_list = [(1, 4, 5), (7, 8), (2, 4, 10)]
 
# printing list
print("The original list : " + str(test_list))
 
# Mathematical Median of Cumulative Records
# Using loop + "~" operator
res = []
for sub in test_list :
  for ele in sub :
    res.append(ele)
res.sort()
mid = len(res) // 2
res = (res[mid] + res[~mid]) / 2
 
# Printing result
print("Median of Records is : " + str(res))


Output : 

The original list : [(1, 4, 5), (7, 8), (2, 4, 10)]
Median of Records is : 4.5

  Method #2 : Using chain() + statistics.median() This is the most generic method to perform this task. In this we directly use inbuilt function after flattening using chain() to perform the median of the flattened list. 

Python3




# Python3 code to demonstrate working of
# Mathematical Median of Cumulative Records
# Using chain() + statistics.median()
import statistics
from itertools import chain
 
# initializing list
test_list = [(1, 4, 5), (7, 8), (2, 4, 10)]
 
# printing list
print("The original list : " + str(test_list))
 
# Mathematical Median of Cumulative Records
# Using chain() + statistics.median()
temp = list(chain(*test_list))
res = statistics.median(temp)
 
# Printing result
print("Median of Records is : " + str(res))


Output : 

The original list : [(1, 4, 5), (7, 8), (2, 4, 10)]
Median of Records is : 4.5

Time complexity: O(nlogn) (due to sorting by median function in statistics module)

Auxiliary space: O(n) (to store the flattened list using chain())

 Method #3 : Using sorted()

Approach

list comprehension to flatten the nested list of records into a single list of elements, and then sort that list using the sorted() function, we check if the length of the sorted records list is odd or even. If it’s odd, then the median is simply the middle element. If it’s even, then the median is the average of the middle two elements. 

Algorithm

1. Define the input list of records as records.

2.  Flatten the list of records into a single list of elements using a list comprehension. To do this, iterate over each sublist in records, and then iterate over each element in the sublist. Append each element to a new list called elements.

3. Sort the elements list using the sorted() function.

4. Calculate the length of the elements list and store it in a variable called n.

5. Check if n is odd or even by checking its remainder when divided by 2 (n % 2). If n is odd, then the median is the middle element of the sorted list. To find the middle element, use integer division (//) to get the index of the middle element (n//2) and then access the element at that index in the sorted list.

6. If n is even, then the median is the average of the two middle elements of the sorted list. To find the two middle elements, use integer division to get the index of the element to the left of the middle (middle = n//2) and then access the elements at middle-1 and middle in the sorted list. Calculate the average of these two elements to get the median.

7.Print the calculated median.

Python3




# Define the input list of records
records = [(1, 4, 5), (7, 8), (2, 4, 10)]
 
# Flatten the list of records into a single list of elements
# using a list comprehension, and sort the resulting list
sorted_records = sorted([item for sublist in records for item in sublist])
 
# Get the length of the sorted records list
n = len(sorted_records)
 
# Check if the length is odd or even, and calculate the median
if n % 2 == 1:
    # If the length is odd, the median is the middle element
    median = sorted_records[n//2]
else:
    # If the length is even, the median is the average of the two middle elements
    middle = n//2
    median = (sorted_records[middle-1] + sorted_records[middle])/2
 
# Print the calculated median
print("Median of Records is :", median)


Output

Median of Records is : 4.5

Time complexity: O(N log N), Flattening the nested list of records into a single list using a list comprehension takes O(N) time, where N is the total number of elements in records. Sorting the resulting list of elements takes O(N log N) time using the built-in sorted() function. Checking if the length of the sorted list is odd or even takes O(1) time.
Calculating the median takes O(1) time since we are accessing specific elements in the sorted list.
Printing the median takes O(1) time.

Space complexity: O(N), The space complexity of the code is O(N), since we create a new list to store the flattened elements of records. The space complexity of sorting the list in place is O(log N) due to the recursive implementation of the sorting algorithm, but the Python sorted() function uses a variant of Timsort which is efficient and typically has a lower memory overhead. Other variables used in the code, such as n, middle, and median, require O(1) space.

Method #4 : Using statistics.median(),extend() methods

Approach

  1. Convert the list of tuples to a single list using extend() method
  2. Find median using built-in method statistics.median()
  3. Display median

Python3




# Python3 code to demonstrate working of
# Mathematical Median of Cumulative Records
 
# initializing list
test_list = [(1, 4, 5), (7, 8), (2, 4, 10)]
 
# printing list
print("The original list : " + str(test_list))
 
# Mathematical Median of Cumulative Records
x=[]
for i in test_list:
    x.extend(list(i))
import statistics
res = statistics.median(x)
 
# Printing result
print("Median of Records is : " + str(res))


Output

The original list : [(1, 4, 5), (7, 8), (2, 4, 10)]
Median of Records is : 4.5

Time Complexity : O(N log N) ,The time complexity of median() method
Auxiliary Space: O(1), The result is stored in a single variable, the complexity is O(1)

Method #5: Using NumPy median function with np.concatenate()

Step-by-step approach:

  1. Import the NumPy library.
  2. Convert the input list of tuples into a NumPy array using the np.array() function.
  3. Flatten the NumPy array into a 1D array using the np.concatenate() function.
  4. Calculate the median of the flattened array using the np.median() function.
  5. Print the result.

Python3




import numpy as np
 
# initializing list
test_list = [(1, 4, 5), (7, 8), (2, 4, 10)]
 
# printing list
print("The original list : " + str(test_list))
 
# Mathematical Median of Cumulative Records
flattened_arr = np.concatenate(test_list)
res = np.median(flattened_arr)
 
# Printing result
print("Median of Records is : " + str(res))


OUTPUT:

The original list : [(1, 4, 5), (7, 8), (2, 4, 10)]
Median of Records is : 4.5

Time complexity:

The time complexity of this approach is O(nlogn) because the np.median() function uses the quickselect algorithm with a worst-case time complexity of O(nlogn).
Auxiliary space:

The auxiliary space complexity of this approach is O(n) because the input list is converted into a NumPy array with the same number of elements, which requires additional memory.

Method #6: Using heapq module

  1. Import the heapq module
  2. Define the input list of records
  3. Flatten the list of records into a single list of elements using a list comprehension
  4. Use the heapify function of heapq module to convert the list into a heap data structure
  5. Get the length of the list of elements
  6. Check if the length is odd or even, and calculate the median
  7. If the length is odd, use the nlargest function of heapq module to get the middle element
  8. If the length is even, use the nlargest function of heapq module to get the two middle elements, and calculate the average of them
  9. Print the calculated median

Python3




import heapq
 
# Define the input list of records
records = [(1, 4, 5), (7, 8), (2, 4, 10)]
 
# Flatten the list of records into a single list of elements
# using a list comprehension
elements = [item for sublist in records for item in sublist]
 
# Convert the list into a heap data structure
heapq.heapify(elements)
 
# Get the length of the list of elements
n = len(elements)
 
# Check if the length is odd or even, and calculate the median
if n % 2 == 1:
    # If the length is odd, the median is the middle element
    median = heapq.nlargest(n//2+1, elements)[-1]
else:
    # If the length is even, the median is the average of the two middle elements
    middle = n//2
    median = (heapq.nlargest(middle+1, elements)[-1] + heapq.nlargest(middle, elements)[-1])/2
 
# Print the calculated median
print("Median of Records is :", median)


Output

Median of Records is : 4.5

Time complexity: O(nlogn)
Auxiliary space: O(n)



Last Updated : 17 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads