Using functional tools
sburrious
sburr at home.com
Tue May 7 13:07:57 EDT 2002
Ken Seehof <kseehof at neuralintegrator.com> wrote in message
> > >>> n = 3
> > >>> a = [1,2,3,4,5,6,7,8,9]
> > >>> [((i+1)%n) and a[i] or f(a[i]) for i in range(len(a))]
> > [1, 2, 30, 4, 5, 60, 7, 8, 90]
>
> I forgot to mention that this won't work if the list contains
> values that evaluate to false. However, since you were in
> the context of reading lines, I figured there's no problem
> (note that an empty line is '\n', which is a nonzero value)
But if you really like obfuscated code and want to cover the
a[i] == 0 case, there's always this:
[(((i+1)%n) and [a[i]] or [f(a[i])])[0] for i in range(len(a))]
More information about the Python-list
mailing list