列表推导没有很好的方法(干净,可移植)来引用其正在构建的列表。一种好的,优雅的方法可能是在生成器中完成该工作:
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))。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)