Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #99474

Re: list slice and generators

From Peter Otten <__peter__@web.de>
Newsgroups comp.lang.python
Subject Re: list slice and generators
Date 2015-11-25 19:26 +0100
Organization None
Message-ID <mailman.85.1448476211.20593.python-list@python.org> (permalink)
References <5654D8CE.2020105@gmail.com> <n3418c$k41$1@ger.gmane.org> <CALwzidmnKPAY+X7tGxPreBXk2JiFnzVAJQ0q-erkcaR7MRtnvw@mail.gmail.com> <CALwzidnFXZEVt4OS2v59WHM_Pn=RbE4B6cDt1HMR5_KstUuzKA@mail.gmail.com>

Show all headers | View raw


Ian Kelly wrote:

> On Wed, Nov 25, 2015 at 10:44 AM, Ian Kelly <ian.g.kelly@gmail.com> wrote:
>> On Wed, Nov 25, 2015 at 3:07 AM, Peter Otten <__peter__@web.de> wrote:
>>>>     elif name in METRICS_AVG:
>>>           # writing a function that calculates the average without
>>>           # materialising the list left as an exercise ;)
>>
>>         metrics = itertools.tee(metrics)
>>         return int(sum(metrics[0]) / len(metrics[1]))
> 
> Actually, don't do that. It does effectively materialize the list,
> just not as a list.

Not just that, it will also complain

>>> a, b = itertools.tee([1,2,3])
>>> sum(a), len(b)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: object of type 'itertools._tee' has no len()

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: list slice and generators Peter Otten <__peter__@web.de> - 2015-11-25 19:26 +0100

csiph-web