Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #83208
| 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 | <python-python-list@m.gmane.org> |
| 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 <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.17374.1420389211.18130.python-list@python.org> (permalink) |
| 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 |
Show key headers only | View raw
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:<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)) 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]
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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