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


Groups > comp.lang.python > #83208

Re: Help with map python 2

From Peter Otten <__peter__@web.de>
Subject Re: Help with map python 2
Date 2015-01-04 17:33 +0100
Organization None
References <60e62263-6cd1-48b9-acbc-f1a0b947b8c9@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.17374.1420389211.18130.python-list@python.org> (permalink)

Show all headers | 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


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