[Tutor] Re: Can you modify every nth item in a list with a single assignment?
Andrei
project5@redrival.net
Sat Jun 14 08:45:01 2003
Jeff Shannon wrote:
> Andrei wrote:
>
>> >>> [ (item in range(0, len(b), 3) and [b[item]] or [2+b[item]])[0]
>> for item in range(0, len(b))]
>> [0, 3, 4, 3, 6, 7, 6, 9, 10]
>>
>> That's better.
>
> Well, "better" being a matter of opinion. ;) This works, sure, but I'd
> rather explicitly build a list of indices, and then explicitly loop
> through that list and modify the primary list -- or else use that list
> to exclude items from processing.
>
I meant better than my original solution (which you left out in your
quote), not better than a multi-line one, as this ons doesn't suffer
from the and/or trick problem which my original solution did have. I
agree that a multi-line solution is more readable, but the topic starter
asked for a one-liner :).
~A.
=====
Mail address in header catches spam. Real contact info (decode with rot13):
cebwrpg5@bcrenznvy.pbz. Fcnz-serr! Cyrnfr qb abg hfr va choyvp cbfgf. V
ernq gur yvfg, fb gurer'f ab arrq gb PP.