How to count lines in a text file ?
Alex Martelli
aleaxit at yahoo.com
Wed Sep 22 17:17:11 EDT 2004
Andrew Dalke <adalke at mindspring.com> wrote:
> Alex Martelli wrote:
> > If one is really in a hurry counting lines, a dedicated C extension
> > might help. E.g.:
> >
> > static PyObject *count(PyObject *self, PyObject *args)
> ...
> > Using this count-items-in-iterable thingy
>
> There's been a few times I've wanted a function like
Me too, that's why I wrote the C and Pyrex versions:-).
> this. I keep expecting that len(iterable) will work,
> but of course it doesn't.
Yep -- it would probably be too risky to have len(...) consume a whole
iterator, beginning users wouldn't expect that and might get burnt.
> Would itertools.len(iterable) be useful? More likely
> the name collision with len itself would be a problem,
> so perhaps itertools.length(iterable).
Unfortunately, itertools's functions are there to produce iterators, not
to consume them. I doubt Raymond Hettinger, itertools' guru, would
approve of changing that (though one could surely ask him, and if he
surprised me, I guess the change might get in).
There's currently no good single place for 'accumulators', i.e.
consumers of iterators which produce scalars or thereabouts -- sum, max,
and min, are built-ins; other useful accumulators can be found in heapq
(because they're implemented via a heap...)... and there's nowhere to
put the obviously needed "trivial" accumulators, such as average,
median, variance, count...
A "stats" module was proposed, but also shot down (presumably people
have more ambitious ideas about 'statistics' than there simple
accumulators, alas -- I'm not sure exactly what the problem was).
Alex
More information about the Python-list
mailing list