Re: Help with map python 2

2015-01-05 Thread Steven D'Aprano
Terry Reedy wrote: > On 1/5/2015 6:33 AM, flebber wrote: >> >>> You could do what mathematicians do when they deal with alternating >>> signs: they raise -1 to the power of the index to get an appropriate >>> multiplier. >>> >>> >>> [ n * (-1) ** n for n in range(10) ] >>> [0, -1, 2, -3, 4

Re: Help with map python 2

2015-01-05 Thread Terry Reedy
On 1/5/2015 6:33 AM, flebber wrote: You could do what mathematicians do when they deal with alternating signs: they raise -1 to the power of the index to get an appropriate multiplier. >>> [ n * (-1) ** n for n in range(10) ] [0, -1, 2, -3, 4, -5, 6, -7, 8, -9] Mathematicians operati

Re: Help with map python 2

2015-01-05 Thread flebber
> In py2, map produces a list already. In any case, above is syntax error > without else clause. > > map(lambda x: x * -1 if x%2 else x, series) > > If you do not have a function already, a list comp is better. > > [(-1*k if k%2 else k) for k in range(2, N)] > > Change [] to () and you have

Re: Help with map python 2

2015-01-05 Thread flebber
> You could do what mathematicians do when they deal with alternating > signs: they raise -1 to the power of the index to get an appropriate > multiplier. > >>>> [ n * (-1) ** n for n in range(10) ] >[0, -1, 2, -3, 4, -5, 6, -7, 8, -9] >>>> > > Or you could do here what you attempt

Re: Help with map python 2

2015-01-04 Thread Sayth Renshaw
Thank you for those solutions so varied. Am going to have a party with them to see what works best for me. Strange is there a way too use lousy comprehension without returning just the matching odd values? Sayth On Mon, 5 Jan 2015 5:06 AM Dan Stromberg wrote: > I'm partial to: > > #!/usr/local

Re: Help with map python 2

2015-01-04 Thread Terry Reedy
On 1/4/2015 6:34 AM, flebber wrote: In repsonse to this question: Write a program that prints the first 100 members of the sequence 2, -3, 4, -5, 6, -7, 8. This is my solution it works but ugly. series = range(2,100) # answer = [(x,(y* -1)) for x, y in series[::2]] # print(answer) answer = []

Re: Help with map python 2

2015-01-04 Thread Dan Stromberg
I'm partial to: #!/usr/local/cpython-3.4/bin/python def gen_series(top=None): ''' Generate the numbers from 2 to top, with alternating signs. If top is None, generate forever. If top is an integer, generate from 2 to top. ''' sign = 1 if top is None: number = 2

Re: Help with map python 2

2015-01-04 Thread Peter Otten
flebber wrote: > In repsonse to this question: Write a program that prints the first 100 > members of the sequence 2, -3, 4, -5, 6, -7, 8. > > This is my solution it works but ugly. > > series = range(2,100) > # answer = [(x,(y* -1)) for x, y in series[::2]] > # print(answer) > answer = [] > for

Re: Help with map python 2

2015-01-04 Thread Jussi Piitulainen
flebber writes: > In repsonse to this question: Write a program that prints the first > 100 members of the sequence 2, -3, 4, -5, 6, -7, 8. > > This is my solution it works but ugly. Seems respectable to me, except that you are taking fewer than 100 elements. But I'd prefer the expressions that

Re: Help with map python 2

2015-01-04 Thread Alec Taylor
^To print the first 8. To print the first 100: map(lambda i: -i if i&1==1 else i, xrange(2, 102)) On Sun, Jan 4, 2015 at 10:47 PM, Alec Taylor wrote: > map(lambda i: -i if i&1==1 else i, xrange(2, 10)) > > On Sun, Jan 4, 2015 at 10:34 PM, flebber wrote: >> In repsonse to this question: Write a p

Re: Help with map python 2

2015-01-04 Thread Alec Taylor
map(lambda i: -i if i&1==1 else i, xrange(2, 10)) On Sun, Jan 4, 2015 at 10:34 PM, flebber wrote: > In repsonse to this question: Write a program that prints the first 100 > members of the sequence 2, -3, 4, -5, 6, -7, 8. > > This is my solution it works but ugly. > > series = range(2,100) > # a

Re: Help with map python 2

2015-01-04 Thread Chris Angelico
On Sun, Jan 4, 2015 at 10:34 PM, flebber wrote: > Just getting something wrong > list(map((lambda x: x * -1 if (x%2 != 0)), series)) Okay, and what happens when you run this? Do you get an exception? If so (and I fully expect you will), copy and paste the entire exception traceback and message. T