[Tutor] Re: Can you modify every nth item in a list with a single assignment?
Andrei
project5@redrival.net
Wed Jun 18 17:41:06 2003
Zak Arntson wrote:
>>Bob Gailer wrote:
>>
>>
>>>>What we need here is a new language feature that lets us provide a
>>>>sequence of index values to a list. Something like:
>>>>
>>>>MyList[(1,4,9,23)] = MyList[(1,4,9,23)] + 43
>
>
> One more!
>
> MyList = [i + 43 for i in (1,4,9,23)]
Hm, orignal post required change every nth item of a list, right?
How about this: I want to multiply every third item with two
>>> b = [1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> [ item in range(2, len(b), 3) and 2*b[item] or b[item] for item in
range(0, len(b))]
[1, 2, 6, 4, 5, 12, 7, 8, 18]
range(2) in order to start multiplying at the third item rather than the
first.
This any good?
Andrei