views:

183

answers:

5

is there a function like a F#'s Seq.scan() in python?

i want to do some cumsum() or cumproduct() kind of things without looping.

+1  A: 

Aggregate functions would use reduce rather than map.

See http://docs.python.org/library/functions.html for more info

ykaganovich
reduce() returns the final value only.. not the list of the intermediate values. So i can get sum(), but not cumsum()
tk
A: 

I'm not sure but give this a try

map(function, iterable, ...)¶

More on this on docs.python

Dänu
+5  A: 

Nope.

def scan(op, seq):
  it = iter(seq)
  result = next(it)
  for val in it:
    result = op(result, val)
    yield result
Ignacio Vazquez-Abrams
You should do one `yield result` before the loop, otherwise the result will be missing the first element.
sepp2k
Yeah, the MS docs were a bit vague as to what `Seq.scan<>() did`.
Ignacio Vazquez-Abrams
Thnx. Actually, F#'s Seq.scan() requires another parameter as an initial state(result).. Thnx.
tk
+3  A: 

Ignacio's solution is almost right I think, but requires a operator of type ('a -> 'a -> 'a) and doesn't yield the first element.

def scan(f, state, it):
  for x in it:
    state = f(state, x)
    yield state
# test
>>> snoc = lambda xs,x: xs+[x]
>>> list(scan(snoc, [], 'abcd'))
[['a'], ['a', 'b'], ['a', 'b', 'c'], ['a', 'b', 'c', 'd']]
>>> list(scan(operator.add, 0, [1,2,3]))
[1,3,6]

Specifically, the type of Seq.scan is

('State -> 'T -> 'State) -> 'State -> seq<'T> -> seq<'State>

The default approach in Python is to write a scan with the type

('State -> 'State -> 'State) -> seq<'State> -> seq<'State>

This comes from the way that Python specifies reduce, which has the same type by default.

Nathan Sanders
+1  A: 

If it's only about doing cumsum/cumprod operations, then you should look at numpy's super efficient cumsum and cumprod operations on arrays.

Olivier