Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > itertools examples

Reply
Thread Tools

itertools examples

 
 
Felipe Almeida Lessa
Guest
Posts: n/a
 
      02-11-2006
Hi,

IMHO, on http://www.python.org/doc/current/li...s-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"

 
Reply With Quote
 
 
 
 
Raymond Hettinger
Guest
Posts: n/a
 
      02-12-2006
[Felipe Almeida Lessa]
> IMHO, on http://www.python.org/doc/current/li...s-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

 
Reply With Quote
 
 
 
 
Felipe Almeida Lessa
Guest
Posts: n/a
 
      02-12-2006
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"

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Good use for itertools.dropwhile and itertools.takewhile Nick Mellor Python 35 12-06-2012 09:29 PM
Fate of itertools.dropwhile() and itertools.takewhile() Raymond Hettinger Python 17 02-18-2008 12:39 PM
COVER LETTERS RESUME EXAMPLES AND CV EXAMPLES AVAILABLE CURRICULUM VITAE rawebadvert3 Computer Support 0 08-21-2007 03:07 AM
COVER LETTERS RESUME EXAMPLES AND CV EXAMPLES AVAILABLE rawebadvert3 Computer Support 0 05-25-2007 04:18 AM
wishlist item: itertools.partition (WAS: Wishlist item: itertools.flatten) Steven Bethard Python 0 03-12-2005 07:50 PM



Advertisments