Path: csiph.com!usenet.pasdenom.info!news.franciliens.net!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!news.tele.dk!news.tele.dk!small.news.tele.dk!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.031 X-Spam-Evidence: '*H*': 0.94; '*S*': 0.00; 'else:': 0.03; 'string': 0.09; '-1)': 0.09; 'builtin': 0.09; 'subject:Help': 0.11; 'python': 0.11; 'jan': 0.12; 'sequence,': 0.16; 'wrong).': 0.16; 'subject:python': 0.16; 'wrote:': 0.18; 'print': 0.22; 'form:': 0.24; 'header:In-Reply-To:1': 0.27; 'to:2**1': 0.27; 'message- id:@mail.gmail.com': 0.30; 'url:mailman': 0.30; 'work.': 0.31; 'getting': 0.31; 'prints': 0.31; 'question:': 0.31; 'url:python': 0.33; 'subject:with': 0.35; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'sequence': 0.36; 'url:listinfo': 0.36; 'doing': 0.36; 'url:org': 0.36; 'should': 0.36; 'wrong': 0.37; 'list': 0.37; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'url:mail': 0.40; 'first': 0.61; 'map': 0.64; 'to:addr:gmail.com': 0.65; 'series': 0.66; '100': 0.79; '2015': 0.84; 'alec': 0.84; 'repsonse': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=CGco5/zfaXX77LauW0i3Ct8MxZoROioe9q+p8FsyH/0=; b=syg5jH+SlEdqm0wFmmSQckwhG7loBUaLeKwOeTtw6ypOUJfIAlQXyhI7QJ6h4dEs5r ZPnEEKxnBRiMEYcqcbaoX+5qFrXuHvuEqBDZdqI0NqLng9enoa8BqJBgYj0zkcFHlWMI uFgvVC5T+LKg57SOyWU8RGb2vLY+FmhwJnpdRkYVVxijHolbawyvJqhb0D2fQq27oXRj WSgb3zW7TYS2wauRSpWJU+3UU8bBGhFDRZs4fKDQCu4g2koLQ+lcl0I7Uu2bOv6yGSAb k2Rr6n+c2MQV9XnoaKttAWA3TowhHoZMRCtSnZCFQadb8Ic7UJh/kzEqCvo1szPolQqx aLOA== MIME-Version: 1.0 X-Received: by 10.224.69.200 with SMTP id a8mr136568632qaj.40.1420372133137; Sun, 04 Jan 2015 03:48:53 -0800 (PST) In-Reply-To: References: <60e62263-6cd1-48b9-acbc-f1a0b947b8c9@googlegroups.com> Date: Sun, 4 Jan 2015 22:48:53 +1100 Subject: Re: Help with map python 2 From: Alec Taylor To: flebber , "comp.lang.python" Content-Type: text/plain; charset=UTF-8 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: 36 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1420372135 news.xs4all.nl 2840 [2001:888:2000:d::a6]:36305 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:83198 ^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 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) >> >> 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)) >> -- >> https://mail.python.org/mailman/listinfo/python-list