杰瑞科技汇

Python列表如何计算中位数?

Of course! Finding the median of a list of numbers in Python is a common task. Here’s a complete guide covering the definition, manual implementation, and the easiest way using Python's built-in libraries.

Python列表如何计算中位数?-图1
(图片来源网络,侵删)

What is the Median?

The median is the middle value in a sorted list of numbers. It's a measure of central tendency.

  • If the list has an odd number of elements: The median is the single middle number.
  • If the list has an even number of elements: The median is the average of the two middle numbers.

Method 1: The Manual "From Scratch" Approach

This method is great for understanding the logic behind finding the median. We'll write a function that handles all the steps.

Steps:

  1. Sort the list: First, we need to arrange the numbers in ascending order.
  2. Find the middle index: We need to determine if the list length is odd or even.
  3. Calculate the median:
    • For an odd-length list: Get the element at the middle index.
    • For an even-length list: Get the two middle elements, add them together, and divide by 2.

Code Implementation:

def find_median_manual(numbers):
    """
    Calculates the median of a list of numbers manually.
    Returns None if the list is empty.
    """
    # 1. Handle edge case: an empty list
    if not numbers:
        return None
    # 2. Sort the list
    sorted_numbers = sorted(numbers)
    n = len(sorted_numbers)
    mid_index = n // 2  # Integer division
    # 3. Check if the number of elements is odd or even
    if n % 2 == 1:
        # Odd number of elements: median is the middle element
        # Example: [1, 2, 3, 4, 5] -> mid_index is 2, value is 3
        median = sorted_numbers[mid_index]
    else:
        # Even number of elements: median is the average of the two middle elements
        # Example: [1, 2, 3, 4, 5, 6] -> mid_index is 3, we need elements at 2 and 3
        middle_element1 = sorted_numbers[mid_index - 1]
        middle_element2 = sorted_numbers[mid_index]
        median = (middle_element1 + middle_element2) / 2
    return median
# --- Examples ---
# Odd number of elements
list_odd = [5, 2, 1, 4, 3]
print(f"List: {list_odd}")
print(f"Median: {find_median_manual(list_odd)}")  # Output: 3
# Even number of elements
list_even = [10, 40, 20, 50, 30, 60]
print(f"\nList: {list_even}")
print(f"Median: {find_median_manual(list_even)}")  # Output: 35.0
# Empty list
list_empty = []
print(f"\nList: {list_empty}")
print(f"Median: {find_median_manual(list_empty)}") # Output: None

Method 2: The Easy Way with Python's statistics Module

For real-world applications, it's almost always better to use Python's built-in statistics module. It's reliable, tested, and requires less code.

The statistics.median() function does all the work for you.

Python列表如何计算中位数?-图2
(图片来源网络,侵删)

Code Implementation:

import statistics
# --- Examples ---
# Odd number of elements
list_odd = [5, 2, 1, 4, 3]
print(f"List: {list_odd}")
print(f"Median: {statistics.median(list_odd)}")  # Output: 3
# Even number of elements
list_even = [10, 40, 20, 50, 30, 60]
print(f"\nList: {list_even}")
print(f"Median: {statistics.median(list_even)}")  # Output: 35
# Empty list (will raise an error)
list_empty = []
try:
    print(f"\nList: {list_empty}")
    print(f"Median: {statistics.median(list_empty)}")
except statistics.StatisticsError as e:
    print(f"Error: {e}") # Output: Error: no median for empty data

Other Useful Functions in the statistics Module:

  • statistics.median_low(): Returns the low median. For an even number of elements, it returns the smaller of the two middle values.
    print(statistics.median_low([1, 3, 5, 7])) # Output: 3
  • statistics.median_high(): Returns the high median. For an even number of elements, it returns the larger of the two middle values.
    print(statistics.median_high([1, 3, 5, 7])) # Output: 5

Method 3: Using NumPy (For Data Science)

If you are working with data science, machine learning, or numerical computing, you are likely using the NumPy library. It's extremely fast and efficient for operations on large arrays.

Code Implementation:

First, make sure you have NumPy installed: pip install numpy

import numpy as np
# --- Examples ---
# Odd number of elements
list_odd = [5, 2, 1, 4, 3]
print(f"List: {list_odd}")
# NumPy requires an array or list-like object
print(f"Median: {np.median(list_odd)}")  # Output: 3.0
# Even number of elements
list_even = [10, 40, 20, 50, 30, 60]
print(f"\nList: {list_even}")
print(f"Median: {np.median(list_even)}")  # Output: 35.0
# NumPy handles empty lists gracefully by returning nan (Not a Number)
list_empty = []
print(f"\nList: {list_empty}")
print(f"Median: {np.median(list_empty)}") # Output: nan

Summary: Which Method Should You Use?

Method When to Use Pros Cons
Manual Learning purposes, coding interviews, or when you can't import libraries. Good for understanding the algorithm. No dependencies. More code, more chance for bugs.
statistics.median() Recommended for general Python scripts. Simple, readable, reliable, part of the standard library. Raises an error for empty lists (which can be good for catching bugs).
numpy.median() Data analysis, scientific computing, or with very large datasets. Extremely fast and memory-efficient for large arrays. Requires an external library (numpy). Overkill for simple tasks.
Python列表如何计算中位数?-图3
(图片来源网络,侵删)
分享:
扫描分享到社交APP
上一篇
下一篇