On 9/28/2010 6:57 PM, kj wrote:
The following attempt to get a list of partial sums fails:
s = 0
[((s += t) and s) for t in range(1, 10)]
File "<stdin>", line 1
[((s += t) and s) for t in range(1, 10)]
^
SyntaxError: invalid syntax
What's the best way to get a list of partial sums?
Do not try to do a reduction with a comprehension. Just write clear,
straightforward code that obviously works.
s=[1,2,3,4,5,6]
def cusum(s):
t = 0
for i in s:
t += i
yield t
print(list(cusum(s)))
>>>
[1, 3, 6, 10, 15, 21]
--
Terry Jan Reedy
--
http://mail.python.org/mailman/listinfo/python-list