面试题答案
一键面试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}")
- 函数说明:
calculate_statistics
函数接受文件路径file_path
和分块大小chunk_size
作为参数。total_sum
用于累计所有块的数值总和,total_count
用于累计数值的总数,sum_of_squares
用于计算方差。- 使用
with open
逐块读取文件,np.loadtxt
每次读取chunk_size
行数据。 - 对于每一块数据,更新
total_sum
,total_count
和sum_of_squares
。 - 最后根据累计的结果计算均值和方差。
- 调用函数:
- 你需要将
'your_file.txt'
替换为实际存储100万个随机整数的文件路径。运行代码后,它将输出整个列表数据的总和、均值和方差。
- 你需要将
如果数值列表不是存储在文件中,而是以流的形式提供,例如网络流等,可根据具体的流读取方式替换np.loadtxt
部分的代码,但整体的统计计算逻辑是类似的。