Re: Modifying every alternate element of a sequence
- From: Roberto Bonvallet <Roberto.Bonvallet@xxxxxxx>
- Date: Tue, 28 Nov 2006 12:14:31 +0000 (UTC)
jm.suresh@xxxxxxxxxxxxxxxxx wrote:
I have a list of numbers and I want to build another list with every[...]
second element multiplied by -1.
But is there any other better way to do this.
I think the best way is the one that uses slices, as somebody suggested
in this thread. This is another (worse) way, just for fun:
>>> from itertools import cycle
>>> input = [1, 2, 3, 4, 5, 6]
>>> wanted = [x * sign for x, sign in zip(input, cycle([1, -1]))]
>>> wanted
[1, -2, 3, -4, 5, -6]
Cheers,
--
Roberto Bonvallet
.
- References:
- Modifying every alternate element of a sequence
- From: jm.suresh@xxxxxxxxxxxxxxxxx
- Modifying every alternate element of a sequence
- Prev by Date: Re: SAX2 Download
- Next by Date: Re: Modifying every alternate element of a sequence
- Previous by thread: Re: Modifying every alternate element of a sequence
- Next by thread: Re: Modifying every alternate element of a sequence
- Index(es):