列表推导没有很好的方法(干净,可移植)来引用其正在构建的列表。一种好的,优雅的方法可能是在生成器中完成该工作:
def running_sum(a): tot = 0 for item in a: tot += item yield tot
要获得此列表,当然可以使用
list(running_sum(a))。

列表推导没有很好的方法(干净,可移植)来引用其正在构建的列表。一种好的,优雅的方法可能是在生成器中完成该工作:
def running_sum(a): tot = 0 for item in a: tot += item yield tot
要获得此列表,当然可以使用
list(running_sum(a))。