itertools examples

Discussion in 'Python' started by Felipe Almeida Lessa, Feb 11, 2006.

  1. Hi,

    IMHO, on http://www.python.org/doc/current/lib/itertools-example.html ,
    shouldn't the part

    >>> for k, g in groupby(enumerate(data), lambda (i,x):i-x):

    .... print map(operator.itemgetter(1), g)

    be

    >>> for k, g in groupby(enumerate(data), lambda (i, x): i-x):

    .... print [i[1] for i in g]

    ?

    AFAIK, list comprehensions are more preferable than map's as they're clearer. Or am I wrong?

    Cya,
    Felipe.

    --
    "Quem excele em empregar a força militar subjulga os exércitos dos
    outros povos sem travar batalha, toma cidades fortificadas dos outros
    povos sem as atacar e destrói os estados dos outros povos sem lutas
    prolongadas. Deve lutar sob o Céu com o propósito primordial da
    'preservação'. Desse modo suas armas não se embotarão, e os ganhos
    poderão ser preservados. Essa é a estratégia para planejar ofensivas."

    -- Sun Tzu, em "A arte da guerra"
    Felipe Almeida Lessa, Feb 11, 2006
    #1
    1. Advertising

  2. [Felipe Almeida Lessa]
    > IMHO, on http://www.python.org/doc/current/lib/itertools-example.html ,
    > shouldn't the part
    >
    > >>> for k, g in groupby(enumerate(data), lambda (i,x):i-x):

    > ... print map(operator.itemgetter(1), g)
    >
    > be
    >
    > >>> for k, g in groupby(enumerate(data), lambda (i, x): i-x):

    > ... print [i[1] for i in g]


    Both work just fine. It's a personal choice when to use map() and when
    to use a list comprehension. Since many itertools have the flavor of
    map/filter, its use is not out of place in the itertools docs.

    Also, the use of map() provided an opportunity to demonstrate
    operator.itemgetter(). While not essential to this example, it is
    helpful with several other tools (especially those with a key=
    argument). Itertools provide a kind of iterator algebra and
    itemgetter() is an essential part of that algebra; hence, it is
    appropriate that it be included in itertool examples.

    If your taste says otherwise, that's okay. Program however you want.
    If reading the examples helped you understand the toolset, then the
    docs accomplished their goal.


    Raymond
    Raymond Hettinger, Feb 12, 2006
    #2
    1. Advertising

  3. Em Sáb, 2006-02-11 às 20:16 -0800, Raymond Hettinger escreveu:
    > Both work just fine. It's a personal choice when to use map() and when
    > to use a list comprehension. Since many itertools have the flavor of
    > map/filter, its use is not out of place in the itertools docs.


    I know both work in the same way, but IIRC I heard someone (GvR?) saying
    list comprehensions should be used when possible to substitute map,
    filter and/or reduce.

    > Also, the use of map() provided an opportunity to demonstrate
    > operator.itemgetter(). While not essential to this example, it is
    > helpful with several other tools (especially those with a key=
    > argument). Itertools provide a kind of iterator algebra and
    > itemgetter() is an essential part of that algebra; hence, it is
    > appropriate that it be included in itertool examples.
    >
    > If your taste says otherwise, that's okay. Program however you want.
    > If reading the examples helped you understand the toolset, then the
    > docs accomplished their goal.


    IMO at a first glance the it's much easier to read and understand the
    list comprehension, but I have to admit that if I didn't see the
    operator.itemgetter(1) there I would probably never known it existed.
    Well, so let's just leave it there, but I'll surely program with the
    list comprehensions ;-).

    Thanks for your attention,
    Felipe.

    >
    > Raymond
    >

    --
    "Quem excele em empregar a força militar subjulga os exércitos dos
    outros povos sem travar batalha, toma cidades fortificadas dos outros
    povos sem as atacar e destrói os estados dos outros povos sem lutas
    prolongadas. Deve lutar sob o Céu com o propósito primordial da
    'preservação'. Desse modo suas armas não se embotarão, e os ganhos
    poderão ser preservados. Essa é a estratégia para planejar ofensivas."

    -- Sun Tzu, em "A arte da guerra"
    Felipe Almeida Lessa, Feb 12, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    869
  2. Dan Williams

    itertools.take?

    Dan Williams, Jul 15, 2003, in forum: Python
    Replies:
    1
    Views:
    355
    Raymond Hettinger
    Jul 15, 2003
  3. Steven Bethard
    Replies:
    0
    Views:
    394
    Steven Bethard
    Mar 12, 2005
  4. Raymond Hettinger
    Replies:
    17
    Views:
    545
    Simon Brunning
    Feb 18, 2008
  5. Nick Mellor
    Replies:
    35
    Views:
    355
    Paul Rubin
    Dec 6, 2012
Loading...

Share This Page