Path: csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'syntax': 0.04; 'string': 0.09; '-1)': 0.09; 'already.': 0.09; 'builtin': 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; 'python': 0.11; 'jan': 0.12; 'already,': 0.16; 'clause.': 0.16; 'comp': 0.16; 'expression.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'sequence,': 0.16; 'wrong).': 0.16; 'subject:python': 0.16; 'wrote:': 0.18; 'header:User-Agent:1': 0.23; 'error': 0.23; 'form:': 0.24; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply- To:1': 0.27; 'function': 0.29; 'am,': 0.29; 'work.': 0.31; 'getting': 0.31; 'prints': 0.31; 'produces': 0.31; 'question:': 0.31; 'subject:with': 0.35; 'something': 0.35; 'case,': 0.35; 'but': 0.35; 'sequence': 0.36; 'doing': 0.36; 'should': 0.36; 'wrong': 0.37; 'list': 0.37; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'number,': 0.60; 'first': 0.61; 'map': 0.64; 'series': 0.66; '100': 0.79; 'received:fios.verizon.net': 0.84; 'repsonse': 0.84; 'received:108': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: Help with map python 2 Date: Sun, 04 Jan 2015 16:13:31 -0500 References: <60e62263-6cd1-48b9-acbc-f1a0b947b8c9@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: <60e62263-6cd1-48b9-acbc-f1a0b947b8c9@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: 48 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1420406037 news.xs4all.nl 2931 [2001:888:2000:d::a6]:52262 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:83220 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 = [] > for item in series: > if item % 2 != 0: > answer.append(item * -1) > else: > answer.append(item) for item in series: if item % 2: # since result is a number, same as != 0 item *= -1 answer.append(item) > print(answer) > > I know I should be better off doing this with map but cannot get it to work. I understand also that map returns a generator so this solution should only working in python2(correct me please if I am wrong). > > In [6]: map? > Type: builtin_function_or_method > String Form: > Namespace: Python builtin > Docstring: > map(function, sequence[, sequence, ...]) -> list > > Just getting something wrong > list(map((lambda x: x * -1 if (x%2 != 0)), series)) 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. -- Terry Jan Reedy