审查生成的付款汇总

来自 map、filter 与 reduce
Python 3.14 高级 8分钟 找出 4处问题

从正确性、边界处理、可读性和数据移动角度审查这条生成的惰性管道。

解析 order_id,amount,status 格式的 CSV 行,保留已付款记录,并返回金额总计与数量。

Python
from functools import reduce

def summarize_payments(lines):
    cleaned = map(str.strip, lines)
    rows = map(lambda line: line.split(","), cleaned)
    paid_rows = filter(lambda row: row[2] == "paid", rows)
    amounts = map(lambda row: float(row[1]), paid_rows)

    def append_amount(acc, amount):
        return acc + [amount]

    paid_amounts = reduce(append_amount, amounts, [])
    total = reduce(lambda acc, amount: acc + amount, paid_amounts)
    return {
        "total": round(total, 2),
        "count": len(paid_amounts),
    }

生成代码仅作示例,不代表任何特定模型

在试验场中打开
报告错误