Leo Kislov:
> input[1::2] = [-item for item in input[1::2]]
> If you don't want to do it in-place, just make a copy:
> wanted = input[:]
> wanted[1::2] = [-item for item in wanted[1::2]]
Very nice solution.
I have tried few versions like:
from itertools import imap, islice
from operator import neg
[EMAIL PROTECTED] wrote:
> Wow, I was in fact searching for this syntax in the python tutorial. It
> is missing there.
> Is there a reference page which documents all possible list
> comprehensions.
There is actually only two forms of list comprehensions:
http://docs.python.org/ref/lists.html
[bl
On Tue, 28 Nov 2006 02:38:09 -0800, [EMAIL PROTECTED] wrote:
> I have a list of numbers and I want to build another list with every
> second element multiplied by -1.
>
> input = [1,2,3,4,5,6]
> wanted = [1,-2,3,-4,5,-6]
>
> I can implement it like this:
>
> input = range(3,12)
> wanted = []
>
[EMAIL PROTECTED] 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)
Wow, I was in fact searching for this syntax in the python tutorial. It
is missing there.
Is there a reference page which documents all possible list
comprehensions.
--
Suresh
Leo Kislov wrote:
> [EMAIL PROTECTED] wrote:
> > I have a list of numbers and I want to build another list with every
> >
On 2006-11-28, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
> I have a list of numbers and I want to build another list with every
> second element multiplied by -1.
>
> input = [1,2,3,4,5,6]
> wanted = [1,-2,3,-4,5,-6]
>
> I can implement it like this:
>
> input = range(3,12)
> wanted = []
> for (
[EMAIL PROTECTED] wrote:
> I have a list of numbers and I want to build another list with every
> second element multiplied by -1.
>
> input = [1,2,3,4,5,6]
> wanted = [1,-2,3,-4,5,-6]
>
> I can implement it like this:
>
> input = range(3,12)
> wanted = []
> for (i,v) in enumerate(input):
> if
[EMAIL PROTECTED] wrote:
> I have a list of numbers and I want to build another list with every
> second element multiplied by -1.
>
> input = [1,2,3,4,5,6]
> wanted = [1,-2,3,-4,5,-6]
>
> I can implement it like this:
>
> input = range(3,12)
> wanted = []
> for (i,v) in enumerate(input):
> if
> I have a list of numbers and I want to build another list with every
> second element multiplied by -1.
>
> input = [1,2,3,4,5,6]
> wanted = [1,-2,3,-4,5,-6]
>
> I can implement it like this:
>
> input = range(3,12)
> wanted = []
> for (i,v) in enumerate(input):
> if i%2 == 0:
> wa
I have a list of numbers and I want to build another list with every
second element multiplied by -1.
input = [1,2,3,4,5,6]
wanted = [1,-2,3,-4,5,-6]
I can implement it like this:
input = range(3,12)
wanted = []
for (i,v) in enumerate(input):
if i%2 == 0:
wanted.append(v)
else:
10 matches
Mail list logo