Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #83229

Re: Help with map python 2

Newsgroups comp.lang.python
Date 2015-01-05 03:37 -0800
References <60e62263-6cd1-48b9-acbc-f1a0b947b8c9@googlegroups.com> <mailman.17382.1420406037.18130.python-list@python.org>
Message-ID <f6e70150-5e6a-4b41-be79-53e888eb4f0d@googlegroups.com> (permalink)
Subject Re: Help with map python 2
From flebber <flebber.crue@gmail.com>

Show all headers | View raw


> 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 a generator expression.

Thanks Terry
[(-1*k if k%2 else k) for k in range(2, N)] 

is really short and I think pythonic being a list comprehension and works in py2 and py3.

Sayth

Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Help with map python 2 flebber <flebber.crue@gmail.com> - 2015-01-04 03:34 -0800
  Re: Help with map python 2 Chris Angelico <rosuav@gmail.com> - 2015-01-04 22:47 +1100
  Re: Help with map python 2 Alec Taylor <alec.taylor6@gmail.com> - 2015-01-04 22:47 +1100
  Re: Help with map python 2 Alec Taylor <alec.taylor6@gmail.com> - 2015-01-04 22:48 +1100
  Re: Help with map python 2 Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2015-01-04 14:08 +0200
    Re: Help with map python 2 flebber <flebber.crue@gmail.com> - 2015-01-05 03:33 -0800
      Re: Help with map python 2 Terry Reedy <tjreedy@udel.edu> - 2015-01-05 18:28 -0500
        Re: Help with map python 2 Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-01-06 12:30 +1100
  Re: Help with map python 2 Peter Otten <__peter__@web.de> - 2015-01-04 17:33 +0100
  Re: Help with map python 2 Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2015-01-04 12:32 -0500
  Re: Help with map python 2 Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2015-01-04 12:41 -0500
  Re: Help with map python 2 Dan Stromberg <drsalists@gmail.com> - 2015-01-04 10:06 -0800
  Re: Help with map python 2 Terry Reedy <tjreedy@udel.edu> - 2015-01-04 16:13 -0500
    Re: Help with map python 2 flebber <flebber.crue@gmail.com> - 2015-01-05 03:37 -0800
  Re: Help with map python 2 Sayth Renshaw <flebber.crue@gmail.com> - 2015-01-04 21:22 +0000

csiph-web