itertools examples

  • Thread starter Felipe Almeida Lessa
  • Start date
F

Felipe Almeida Lessa

Hi,

IMHO, on http://www.python.org/doc/current/lib/itertools-example.html ,
shouldn't the part
.... print map(operator.itemgetter(1), g)

be
.... 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"
 
R

Raymond Hettinger

[Felipe Almeida Lessa]
IMHO, on http://www.python.org/doc/current/lib/itertools-example.html ,
shouldn't the part
... print map(operator.itemgetter(1), g)

be
... 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
 
F

Felipe Almeida Lessa

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.
--
"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"
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top