MST

星途 面试题库

面试题:Python数值列表简单统计计算之高级题

假设你有一个非常大的数值列表(例如包含100万个随机整数),内存有限,无法一次性将整个列表加载到内存中。请设计一个Python程序,分块读取列表数据,并计算整个列表数据的总和、均值和方差,尽可能高效地实现。
13.9万 热度难度
编程语言Python

知识考点

AI 面试

面试题答案

一键面试
import math
import numpy as np


def calculate_statistics(file_path, chunk_size=1000):
    total_sum = 0
    total_count = 0
    sum_of_squares = 0
    with open(file_path, 'r') as file:
        while True:
            chunk = np.loadtxt(file, max_rows=chunk_size)
            if not chunk.size:
                break
            total_sum += np.sum(chunk)
            total_count += len(chunk)
            sum_of_squares += np.sum(chunk ** 2)
    mean = total_sum / total_count if total_count > 0 else 0
    variance = (sum_of_squares / total_count) - mean ** 2 if total_count > 0 else 0
    return total_sum, mean, variance


你可以这样使用这个函数:

file_path = 'your_file.txt'  # 包含数值列表的文件路径
total_sum, mean, variance = calculate_statistics(file_path)
print(f"总和: {total_sum}")
print(f"均值: {mean}")
print(f"方差: {variance}")


  1. 函数说明
    • calculate_statistics函数接受文件路径file_path和分块大小chunk_size作为参数。
    • total_sum用于累计所有块的数值总和,total_count用于累计数值的总数,sum_of_squares用于计算方差。
    • 使用with open逐块读取文件,np.loadtxt每次读取chunk_size行数据。
    • 对于每一块数据,更新total_sumtotal_countsum_of_squares
    • 最后根据累计的结果计算均值和方差。
  2. 调用函数
    • 你需要将'your_file.txt'替换为实际存储100万个随机整数的文件路径。运行代码后,它将输出整个列表数据的总和、均值和方差。

如果数值列表不是存储在文件中,而是以流的形式提供,例如网络流等,可根据具体的流读取方式替换np.loadtxt部分的代码,但整体的统计计算逻辑是类似的。