Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #83258
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Subject | Re: Help with map python 2 |
| Date | 2015-01-05 18:28 -0500 |
| References | <60e62263-6cd1-48b9-acbc-f1a0b947b8c9@googlegroups.com> <qotoaqe7nol.fsf@ruuvi.it.helsinki.fi> <597df0f8-234a-468c-bb48-1954db491b3c@googlegroups.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.17406.1420500655.18130.python-list@python.org> (permalink) |
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 operating in the timeless Platonic universe of mathemtical forms are not concerned with such droll issues as computation time. I think most regard '(-1)**n' as an abbreviation for '(-1 if odd(n) else 1)', where odd(n) is regarded as an O(1) operation, not as an indication to actually raise -1 to an arbitrarily large power. which could take an arbitrarily long time. >> Or you could do here what you attempt to do with map below. See below. >> > >> >> You are trying to use a binary expression. There are no binary >> expressions. Add an else branch to make it ternary: >> >> lambda x : x if x % 2 == 0 else -x >> >> But never mind the number of branches, the serious point is that you >> didn't specify a value for when the condition is not true. It doesn't >> make sense without that. >> >> There's nothing wrong with a list comprehension, or the corresponding >> generator expression if you want a generator. It's fine. Map's fine. > > Thanks Jussi I really like the multiplier solution. > -- Terry Jan Reedy
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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