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


Groups > comp.lang.python > #99467 > unrolled thread

Re: list slice and generators

Started byIan Kelly <ian.g.kelly@gmail.com>
First post2015-11-25 10:44 -0700
Last post2015-11-25 10:44 -0700
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: list slice and generators Ian Kelly <ian.g.kelly@gmail.com> - 2015-11-25 10:44 -0700

#99467 — Re: list slice and generators

FromIan Kelly <ian.g.kelly@gmail.com>
Date2015-11-25 10:44 -0700
SubjectRe: list slice and generators
Message-ID<mailman.80.1448473524.20593.python-list@python.org>
On Wed, Nov 25, 2015 at 3:07 AM, Peter Otten <__peter__@web.de> wrote:
> to get down to one intermediate list. Avoiding the last one is a bit tricky:
>
> metrics = (converter(x.metric(name)) for x in self._server_per_proc)
> metrics = (x for x in metrics if x is not None)
> try:
>     # if there is at least one item the generator is not empty
>     first = next(metrics)
> except StopIteration:
>     metrics = ()
> else:
>     # put the first item back in
>     metrics = itertools.chain([first], metrics)
>     assert metrics

metrics is always going to be an itertools.chain object at this
assert, so how could the assertion ever fail?

>>     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]))

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web