python - List of function of subset of a list -
so i've got list, say, l = [0,1,2,3]; apply function (with 2 arguments) each of values of sublists [0,1], [1,2], [2,3] , in turn produce list of new values. i.e. [f(0,1), f(1,2), f(2,3)]
i've looked on , cannot seem find answer.
any appreciated,
thanks, dave
edit: i'm using python.
result = [f(*args) args in zip(l, l[1:])]
or:
result = map(f, l[:-1], l[1:])
a lazy version using itertools functions generate results on demand:
it = starmap(f, izip(l, islice(l, 1, none)))
or:
it = imap(f, l, islice(l, 1, none))
or if l
arbitrary iterable:
a, b = tee(l) = imap(f, a, islice(b, 1, none))
Comments
Post a Comment