Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'binary': 0.07; '[0,': 0.09; 'indication': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:Help': 0.11; 'jan': 0.12; '-1,': 0.16; 'arbitrarily': 0.16; 'expression.': 0.16; 'expressions.': 0.16; 'fine.': 0.16; 'generator.': 0.16; 'lambda': 0.16; 'operation,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'subject:python': 0.16; 'index': 0.16; 'appropriate': 0.16; 'wrote:': 0.18; 'trying': 0.19; 'solution.': 0.20; '>>>': 0.22; 'header:User- Agent:1': 0.23; 'specify': 0.24; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'am,': 0.29; 'raise': 0.29; "doesn't": 0.30; 'that.': 0.31; 'sense': 0.34; 'could': 0.34; 'subject:with': 0.35; 'but': 0.35; 'add': 0.35; 'there': 0.35; 'really': 0.36; "didn't": 0.36; 'thanks': 0.36; 'wrong': 0.37; 'operating': 0.37; 'list': 0.37; 'branch': 0.38; 'to:addr :python-list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'expression': 0.60; 'most': 0.60; 'such': 0.63; 'map': 0.64; 'here': 0.66; 'below.': 0.71; 'power': 0.76; 'received:fios.verizon.net': 0.84; 'regarded': 0.84; 'timeless': 0.84; 'universe': 0.84; 'received:108': 0.93; 'serious': 0.97 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: Help with map python 2 Date: Mon, 05 Jan 2015 18:28:40 -0500 References: <60e62263-6cd1-48b9-acbc-f1a0b947b8c9@googlegroups.com> <597df0f8-234a-468c-bb48-1954db491b3c@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-108-16-203-145.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.3.0 In-Reply-To: <597df0f8-234a-468c-bb48-1954db491b3c@googlegroups.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 40 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1420500655 news.xs4all.nl 2839 [2001:888:2000:d::a6]:57266 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:83258 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