Re: count items in generator

2006-05-15 Thread Bruno Desthuilliers
George Sakkis a écrit : (snip) > def length(iterable): > try: return len(iterable) > except: except TypeError: > i = 0 > for x in iterable: i += 1 > return i > (snip) -- http://mail.python.org/mailman/listinfo/python-list

Re: count items in generator

2006-05-15 Thread Cameron Laird
In article <[EMAIL PROTECTED]>, Alex Martelli <[EMAIL PROTECTED]> wrote: . . . >I'd be a bit worried about having len(x) change x's state into an >unusable one. Yes, it happens in other cases (if y in x:), but adding >more such

Re: count items in generator

2006-05-15 Thread Cameron Laird
In article <[EMAIL PROTECTED]>, Alex Martelli <[EMAIL PROTECTED]> wrote: >Cameron Laird <[EMAIL PROTECTED]> wrote: > >> In article <[EMAIL PROTECTED]>, >> Alex Martelli <[EMAIL PROTECTED]> wrote: >> . >> . >> . >> >My preference woul

RE: count items in generator

2006-05-14 Thread Delaney, Timothy (Tim)
George Sakkis wrote: > Delaney, Timothy (Tim) wrote: >> >> list(itertools.count()) will eventually fail with a MemoryError. > > That's more of a theoretical argument on why the latter is worse. How > many real-world programs are prepared for MemoryError every time they > call list(), catch it an

Re: count items in generator

2006-05-14 Thread George Sakkis
Delaney, Timothy (Tim) wrote: > George Sakkis wrote: > > > Paul Rubin wrote: > > > >> [EMAIL PROTECTED] (Cameron Laird) writes: > >>> For that matter, would it be an advantage for len() to operate > >>> on iterables? > >> > >>print len(itertools.count()) > >> > >> Ouch!! > > > > How is this wo

RE: count items in generator

2006-05-14 Thread Delaney, Timothy (Tim)
Paul Rubin wrote: > That's only because itertools.count itself uses a C int instead of a > long. True. In either case, the effect is the same in terms of whether len(itertools.count()) will ever terminate. Tim Delaney -- http://mail.python.org/mailman/listinfo/python-list

Re: count items in generator

2006-05-14 Thread Paul Rubin
"Delaney, Timothy (Tim)" <[EMAIL PROTECTED]> writes: > > Actually len(itertools.count()) would as well - when a couple of long > > instances used up everything available - but it would take a *lot* > > longer. > > Actually, this would depend on whether len(iterable) used a C integral > variable to

RE: count items in generator

2006-05-14 Thread Delaney, Timothy (Tim)
Delaney, Timothy (Tim) wrote: > Actually len(itertools.count()) would as well - when a couple of long > instances used up everything available - but it would take a *lot* > longer. Actually, this would depend on whether len(iterable) used a C integral variable to accumulate the length (which woul

RE: count items in generator

2006-05-14 Thread Delaney, Timothy (Tim)
George Sakkis wrote: > Paul Rubin wrote: > >> [EMAIL PROTECTED] (Cameron Laird) writes: >>> For that matter, would it be an advantage for len() to operate >>> on iterables? >> >>print len(itertools.count()) >> >> Ouch!! > > How is this worse than list(itertools.count()) ? list(itertools.c

Re: count items in generator

2006-05-14 Thread Alex Martelli
George Sakkis <[EMAIL PROTECTED]> wrote: > Paul Rubin wrote: > > > [EMAIL PROTECTED] (Cameron Laird) writes: > > > For that matter, would it be an advantage for len() to operate > > > on iterables? > > > >print len(itertools.count()) > > > > Ouch!! > > How is this worse than list(itertools.

Re: count items in generator

2006-05-14 Thread Alex Martelli
Cameron Laird <[EMAIL PROTECTED]> wrote: > In article <[EMAIL PROTECTED]>, > Alex Martelli <[EMAIL PROTECTED]> wrote: > . > . > . > >My preference would be (with the original definition for > >words_of_the_file) to code > > > > nu

Re: count items in generator

2006-05-14 Thread George Sakkis
Paul Rubin wrote: > [EMAIL PROTECTED] (Cameron Laird) writes: > > For that matter, would it be an advantage for len() to operate > > on iterables? > >print len(itertools.count()) > > Ouch!! How is this worse than list(itertools.count()) ? -- http://mail.python.org/mailman/listinfo/python-l

Re: count items in generator

2006-05-14 Thread BartlebyScrivener
>> True. Changing the except clause here to >> except: return sum(1 for x in iterable) >> keeps George's optimization (O(1), not O(N), for containers) and is a >> bit faster (while still O(N)) for non-container iterables. Every thing was going just great. Now I have to think again. Thank you a

Re: count items in generator

2006-05-14 Thread Paul Rubin
[EMAIL PROTECTED] (Cameron Laird) writes: > For that matter, would it be an advantage for len() to operate > on iterables? print len(itertools.count()) Ouch!! -- http://mail.python.org/mailman/listinfo/python-list

Re: count items in generator

2006-05-14 Thread Cameron Laird
In article <[EMAIL PROTECTED]>, Alex Martelli <[EMAIL PROTECTED]> wrote: . . . >My preference would be (with the original definition for >words_of_the_file) to code > > numwords = sum(1 for w in words_of_the_file(thefilepath)

Re: count items in generator

2006-05-14 Thread Alex Martelli
Paul Rubin wrote: > "George Sakkis" <[EMAIL PROTECTED]> writes: > > As clunky as it seems, I don't think you can beat it in terms of > > brevity; if you care about memory efficiency though, here's what I use: > > > > def length(iterable): > > try: return len(iterabl

Re: count items in generator

2006-05-13 Thread Paul Rubin
"George Sakkis" <[EMAIL PROTECTED]> writes: > As clunky as it seems, I don't think you can beat it in terms of > brevity; if you care about memory efficiency though, here's what I use: > > def length(iterable): > try: return len(iterable) > except: > i = 0 > for x in iterab

Re: count items in generator

2006-05-13 Thread BartlebyScrivener
Thanks! And thanks for the Cookbook. rd "There is no abstract art. You must always start with something. Afterward you can remove all traces of reality."--Pablo Picasso -- http://mail.python.org/mailman/listinfo/python-list

Re: count items in generator

2006-05-13 Thread George Sakkis
BartlebyScrivener wrote: > Still new. I am trying to make a simple word count script. > > I found this in the great Python Cookbook, which allows me to process > every word in a file. But how do I use it to count the items generated? > > def words_of_file(thefilepath, line_to_words=str.split): >

Re: count items in generator

2006-05-13 Thread Alex Martelli
BartlebyScrivener <[EMAIL PROTECTED]> wrote: > Still new. I am trying to make a simple word count script. > > I found this in the great Python Cookbook, which allows me to process > every word in a file. But how do I use it to count the items generated? > > def words_of_file(thefilepath, line_to