Path: csiph.com!usenet.pasdenom.info!news.etla.org!news.stack.nl!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; 'operator': 0.03; 'list...': 0.07; 'string': 0.09; '-1)': 0.09; '[1,': 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; 'def': 0.12; '-1,': 0.16; '[2,': 0.16; 'itertools': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'sequence,': 0.16; 'wrong).': 0.16; 'subject:python': 0.16; 'wrote:': 0.18; '>>>': 0.22; 'import': 0.22; 'header:User-Agent:1': 0.23; 'form:': 0.24; 'header:X-Complaints-To:1': 0.27; 'work.': 0.31; 'getting': 0.31; 'factor': 0.31; 'prints': 0.31; 'question:': 0.31; 'another': 0.32; 'problem': 0.35; 'subject:with': 0.35; 'something': 0.35; 'but': 0.35; 'add': 0.35; 'sequence': 0.36; 'doing': 0.36; 'should': 0.36; 'members.': 0.37; 'wrong': 0.37; 'list': 0.37; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'expression': 0.60; 'first': 0.61; 'back': 0.62; 'map': 0.64; 'finally': 0.65; 'series': 0.66; '100': 0.79; 'repsonse': 0.84; 'stuff:': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Help with map python 2 Date: Sun, 04 Jan 2015 17:33:14 +0100 Organization: None References: <60e62263-6cd1-48b9-acbc-f1a0b947b8c9@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p57bda966.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 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: 71 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1420389211 news.xs4all.nl 2849 [2001:888:2000:d::a6]:56543 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:83208 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) Hm, that's the only first 98 members. > > 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)) Here's another way to look at the problem -- start with an infinite series and then slice it: >>> from itertools import * >>> list(islice(count(2), 10)) [2, 3, 4, 5, 6, 7, 8, 9, 10, 11] Factor out the slicing to focus on the important stuff: >>> def first_ten(items): ... return list(islice(items, 10)) ... >>> first_ten(count(2)) Now add the alternating sign: >>> first_ten(cycle([1, -1])) [1, -1, 1, -1, 1, -1, 1, -1, 1, -1] Combine the two: >>> from operator import mul >>> first_ten(imap(mul, cycle([1, -1]), count(2))) [2, -3, 4, -5, 6, -7, 8, -9, 10, -11] It is important that you use itertools.imap() in Python 2 because the map() builtin would try to build the infinite list... Finally put the slicing back into the expression if you like: >>> list(islice(imap(mul, cycle([1, -1]), count(2)), 10)) [2, -3, 4, -5, 6, -7, 8, -9, 10, -11]