Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #83217

Re: Help with map python 2

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <drsalists@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'none,': 0.07; 'none:': 0.07; 'string': 0.09; '-1)': 0.09; 'builtin': 0.09; 'main()': 0.09; 'skip:# 30': 0.09; 'cc:addr:python-list': 0.11; 'subject:Help': 0.11; 'python': 0.11; 'def': 0.12; 'jan': 0.12; "'''": 0.16; 'cc:name:python list': 0.16; 'integer,': 0.16; 'main():': 0.16; 'sequence,': 0.16; 'true:': 0.16; 'wrong).': 0.16; 'subject:python': 0.16; 'wrote:': 0.18; 'cc:addr:python.org': 0.22; 'form:': 0.24; 'cc:2**0': 0.24; 'header:In-Reply-To:1': 0.27; 'on,': 0.29; 'am,': 0.29; 'message- id:@mail.gmail.com': 0.30; "i'm": 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; 'yield': 0.36; 'url:listinfo': 0.36; 'doing': 0.36; 'url:org': 0.36; 'should': 0.36; 'wrong': 0.37; 'list': 0.37; 'clear': 0.37; 'url:mail': 0.40; 'numbers': 0.61; 'first': 0.61; 'map': 0.64; 'to:addr:gmail.com': 0.65; 'series': 0.66; '100': 0.79; '2015': 0.84; 'partial': 0.84; 'repsonse': 0.84; 'top.': 0.84; 'forever.': 0.91
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 :cc:content-type; bh=PcmCOlebSLQiZgOEAMGYcJCoNDvSOqPGO4u4ICEPdmM=; b=F0u3aZ/RjUotB4ytCs7otWz/gDdD02x6Svk8Mb11Ioj1BFOTGJ2FByEHNdeBUvqyl+ eQASwjtWHgojxDXhwfPD0zf9UGKJgTCGCKgQcTeQIsJhVKeAIqLYn9OTU4w0r1oyIqXs nxguN/vc7+JNwjyf0NmNERXOvstvyVfoq17W5Haee21NvJdy9CjlBRIPRyMcjyGwj8Rz N+C8zo/1gsHccR7W5Kle9RZCZaLK9qzxhr56dUCEYvRZygGd+zrd5dkLgHfWHXIA9BHe kdo2iI0CuXblZ4F90gIs7LGcvtDfB1gvm/SZPlvcAsKWIUqQxlxDH7c+OsBTUCHbzu5c 23BQ==
MIME-Version 1.0
X-Received by 10.112.150.194 with SMTP id uk2mr45849688lbb.84.1420394766336; Sun, 04 Jan 2015 10:06:06 -0800 (PST)
In-Reply-To <60e62263-6cd1-48b9-acbc-f1a0b947b8c9@googlegroups.com>
References <60e62263-6cd1-48b9-acbc-f1a0b947b8c9@googlegroups.com>
Date Sun, 4 Jan 2015 10:06:06 -0800
Subject Re: Help with map python 2
From Dan Stromberg <drsalists@gmail.com>
To flebber <flebber.crue@gmail.com>
Content-Type text/plain; charset=UTF-8
Cc Python List <python-list@python.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.17380.1420394768.18130.python-list@python.org> (permalink)
Lines 64
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1420394768 news.xs4all.nl 2918 [2001:888:2000:d::a6]:58319
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:83217

Show key headers only | View raw


I'm partial to:

#!/usr/local/cpython-3.4/bin/python

def gen_series(top=None):
    '''
    Generate the numbers from 2 to top, with alternating signs.
    If top is None, generate forever.
    If top is an integer, generate from 2 to top.
    '''
    sign = 1
    if top is None:
        number = 2
        while True:
            yield number * sign
            number += 1
            sign = -sign
    else:
        for number in range(2, top):
            yield number * sign
            sign = -sign


def main():
    list_ = list(gen_series(100))
    print(list_)

main()


It's not the shortest, but it's pretty clear what's going on, and
gen_series() is reusable.


On Sun, Jan 4, 2015 at 3:34 AM, flebber <flebber.crue@gmail.com> 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:<built-in function map>
> 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

Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


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