473,466 Members | 1,389 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

itertools examples

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"

Feb 11 '06 #1
2 2635
[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

Feb 12 '06 #2
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"

Feb 12 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Dan Williams | last post by:
Is there any interest in adding a "Haskellish" take function to itertools? I know its easy enough to roll my own, I've been using: take = lambda iter, n: which works, but I was wondering if...
10
by: Jeremy Fincher | last post by:
Sometimes I find myself simply wanting the length of an iterator. For example, to collect some (somewhat useless ;)) statistics about a program of mine, I've got code like this: objs =...
23
by: Francis Avila | last post by:
Below is an implementation a 'flattening' recursive generator (take a nested iterator and remove all its nesting). Is this possibly general and useful enough to be included in itertools? (I know...
1
by: Steven Bethard | last post by:
Is there a reason that itertools.islice doesn't support None arguments for start and step? This would be handy for use with slice objects: >>> r = range(20) >>> s1 = slice(2, 10, 2) >>> s2 =...
18
by: Ville Vainio | last post by:
For quick-and-dirty stuff, it's often convenient to flatten a sequence (which perl does, surprise surprise, by default): ]]] -> One such implementation is at ...
21
by: Steven Bethard | last post by:
Jack Diederich wrote: > > itertools to iter transition, huh? I slipped that one in, I mentioned > it to Raymond at PyCon and he didn't flinch. It would be nice not to > have to sprinkle 'import...
41
by: rurpy | last post by:
The code below should be pretty self-explanatory. I want to read two files in parallel, so that I can print corresponding lines from each, side by side. itertools.izip() seems the obvious way to...
3
by: Daniel Nogradi | last post by:
In a recent thread, http://mail.python.org/pipermail/python-list/2006-September/361512.html, a couple of very useful and enlightening itertools examples were given and was wondering if my problem...
13
by: 7stud | last post by:
Bejeezus. The description of groupby in the docs is a poster child for why the docs need user comments. Can someone explain to me in what sense the name 'uniquekeys' is used this example: ...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.