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 manipulate
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__):
[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):
if
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:
def
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
--
[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 _ in
Duncan I think I'd prefer the barbaric:
Duncanreturn 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
--
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
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
--
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 tested before
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()
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 my
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 the
# 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
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.
xrange
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 iteranything to be extremely ugly and hope to avoid using them
altogether until they are gone in Py3k.
Skip
--
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 iteranything to be extremely ugly and hope to avoid using them
I find iteranything 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
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 container
19 matches
Mail list logo