Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote:
> In <[EMAIL PROTECTED]>, skip wrote:
>
>> Are you sure? There's no guarantee that an iterator will terminate:
>>
>> len(list(itertools.cycle(range(10
>
> You have infinite memory? ;-)
Strangely, Skip's example is exactly the one I
Alex Martelli:
> Right. However, "return sum(1 for _ in iterator)" may be a handier way
> to express the same desctructive semantics as the last 4 lines here.
With the speed tests I have done my version did come out as the faster
one.
Bye,
bearophile
--
http://mail.python.org/mailman/listinfo
In <[EMAIL PROTECTED]>, skip wrote:
> Are you sure? There's no guarantee that an iterator will terminate:
>
> len(list(itertools.cycle(range(10
You have infinite memory? ;-)
Ciao,
Marc 'BlackJack' Rintsch
--
http://mail.python.org/mailman/listinfo/python-list
Duncan> I think I'd prefer the barbaric:
Duncan>return len(list(iterator))
Duncan> since at least it is guaranteed to terminate.
Are you sure? There's no guarantee that an iterator will terminate:
len(list(itertools.cycle(range(10
Skip
--
http://mail.python.org/mailm
[EMAIL PROTECTED] (Alex Martelli) wrote:
>> Of course this is a little like the Heisenberg uncertainty principle if
>> the iterator has no __len__ attribute - once you know how long it is you
>> no longer have access to the elements. Or did I miss something?
>
> Right. However, "return sum(1 for
Steve Holden <[EMAIL PROTECTED]> wrote:
> [EMAIL PROTECTED] wrote:
> > Laurent Pointal:
> >> you may prefer range/items when processing of the result
> >> value explicitly need a list (ex. calculate its length)
> >
> > Creating a very long list just to know the len of an iterator is
> > barbaric,
Steve Holden:
> once you know how long it is you
> no longer have access to the elements. Or did I miss something?
Now and then I need to know how many elements there are, and not what
they are, so in those situations storing them isn't necessary.
Bye,
bearophile
--
http://mail.python.org/mailm
Steve Holden a écrit :
> [EMAIL PROTECTED] wrote:
>> Laurent Pointal:
>>> you may prefer range/items when processing of the result
>>> value explicitly need a list (ex. calculate its length)
>>
>> Creating a very long list just to know the len of an iterator is
>> barbaric, so sometimes I use this:
[EMAIL PROTECTED] wrote:
> Laurent Pointal:
>> you may prefer range/items when processing of the result
>> value explicitly need a list (ex. calculate its length)
>
> Creating a very long list just to know the len of an iterator is
> barbaric, so sometimes I use this:
>
> def leniter(iterator):
>
Laurent Pointal:
> you may prefer range/items when processing of the result
> value explicitly need a list (ex. calculate its length)
Creating a very long list just to know the len of an iterator is
barbaric, so sometimes I use this:
def leniter(iterator):
if hasattr(iterator, "__len__"):
Paul Rubin a écrit :
> Laurent Pointal <[EMAIL PROTECTED]> writes:
>> Both work, you may prefer xrange/iteritems for iteration on large
>> collections, you may prefer range/items when processing of the result
>> value explicitly need a list (ex. calculate its length) or when you are
>> going to man
Laurent Pointal <[EMAIL PROTECTED]> writes:
> Both work, you may prefer xrange/iteritems for iteration on large
> collections, you may prefer range/items when processing of the result
> value explicitly need a list (ex. calculate its length) or when you are
> going to manipulate the original contai
>> I find "iter" to be extremely ugly and hope to avoid using
>> them altogether until they are gone in Py3k.
Drew> Ugly, maybe, but don't you take a decent performance hit when
Drew> loading the entire dict into memory at once? Especially if the
Drew> dict is large?
Sure, but
On Mar 14, 2:53 pm, [EMAIL PROTECTED] wrote:
> >> When is it appropriate to use dict.items() vs dict.iteritems.
>
> Laurent> Both work, you may prefer xrange/iteritems for iteration on
> Laurent> large collections...
>
> I find "iter" to be extremely ugly and hope to avoid using them
>
>> When is it appropriate to use dict.items() vs dict.iteritems.
Laurent> Both work, you may prefer xrange/iteritems for iteration on
Laurent> large collections...
I find "iter" to be extremely ugly and hope to avoid using them
altogether until they are gone in Py3k.
Skip
--
http:/
Laurent Pointal wrote:
> Both work, you may prefer xrange/iteritems for iteration on large
> collections, you may prefer range/items when processing of the result
> value explicitly need a list (ex. calculate its length) or when you are
> going to manipulate the original container in the loop.
xra
# Just by looking at the output, it seems pretty obvious that xrange
would be more memory effcient for large ranges:
print "With range():",range(100,200)
print
print "With xrange():",xrange(100,200)
d = {1:2,2:3,3:4}
d.items()
d.iteritems()
# I have been curious to use Pysizer (which requires
On Mar 14, 11:44 am, Laurent Pointal <[EMAIL PROTECTED]> wrote:
> Both work, you may prefer xrange/iteritems for iteration on large
> collections, you may prefer range/items when processing of the result
> value explicitly need a list (ex. calculate its length) or when you are
> going to manipulate
Drew a écrit :
> When is it appropriate to use dict.items() vs dict.iteritems. Both
> seem to work for something like:
>
> for key,val in mydict.items():
> print key,val
>
> for key,val in mydict.iteritems():
> print key,val
>
> Also, when is it appropriate to use range() vs xrange(). From m
When is it appropriate to use dict.items() vs dict.iteritems. Both
seem to work for something like:
for key,val in mydict.items():
print key,val
for key,val in mydict.iteritems():
print key,val
Also, when is it appropriate to use range() vs xrange(). From my
understanding, xrange() essential
20 matches
Mail list logo