[Tutor] Can you modify every nth item in a list with a single
assignment?
Jeff Shannon
jeff@ccvcorp.com
Thu Jun 12 20:48:01 2003
Gus Tabares 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
>>>
>>>
>
>
>
>>This can be done easily enough without adding a new feature, it just
>>takes two lines instead of one
>>
>>
>
>
>Or another healthy alternative, on one line:
>
>map((lambda x: x + 43), [1, 4, 9, 23])
>
>
This doesn't do the same thing.
>>> map((lambda x: x + 43), [1, 4, 9, 23])
[44, 47, 52, 66]
>>>
You're adding something to each member of a list, whereas the first
example is modifying only specified members of another list, using the
tuple of integers to specify which list elements are modified. It
probably would be possible to come up with something equivalent using
map, lambda, and filter, but it'd be pretty ugly.
Jeff Shannon
Technician/Programmer
Credit International