You can rewrite it like this...
For:
lambda args: expression_with_args
a function would look like:
def func_name(args):
return expression_with_args
E.g.
lambda p: p[0] < n
becomes:
def compare_func(p, n):
return p[0] < n
to use it as a function for key
, which takes only one argument it has to be modified a little to save the value of n
:
compare_func = ft.partial(compare_func, n=n)
E.g.
lambda p: p[1]
becomes:
def get_first(p):
return p[1]
E.g.
lambda k, vs: (k, sum(map(lambda i: 1, vs)))
becomes:
def as_one(i):
return 1
def get_tuple(k, vs):
vs_with_ones = map(as_one, vs)
vs_sum = sum(vs_with_ones)
return (k, vs_sum)
Finally:
def foo(los, n=None):
n = n or len(los)
compare_func = ft.partial(compare_func, n=n)
h = it.takewhile(compare_func, enumerate(los))
s = sorted(h, key=get_first)
g = it.groupby(s, get_first)
return dict(it.starmap(get_tuple, g))