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


Groups > comp.lang.python > #99467

Re: list slice and generators

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Ian Kelly <ian.g.kelly@gmail.com>
Newsgroups comp.lang.python
Subject Re: list slice and generators
Date Wed, 25 Nov 2015 10:44:35 -0700
Lines 24
Message-ID <mailman.80.1448473524.20593.python-list@python.org> (permalink)
References <5654D8CE.2020105@gmail.com> <n3418c$k41$1@ger.gmane.org>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de vNJxkV343zq6UuHFxU+PLAyt1Ylt7TMB8K73H4C6D5yw==
Return-Path <ian.g.kelly@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.007
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; 'exercise': 0.03; 'elif': 0.04; 'none)': 0.07; 'metrics': 0.09; 'wed,': 0.15; 'intermediate': 0.15; 'calculates': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; 'try:': 0.18; '2015': 0.20; 'am,': 0.23; 'bit': 0.23; 'header:In-Reply-To:1': 0.24; 'subject:list': 0.26; 'least': 0.27; 'message-id:@mail.gmail.com': 0.27; 'function': 0.28; 'skip:( 20': 0.28; 'avoiding': 0.33; 'except': 0.34; 'list': 0.34; 'received:google.com': 0.35; 'could': 0.35; 'nov': 0.35; 'item': 0.35; 'skip:i 20': 0.36; 'there': 0.36; 'received:209.85': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:209.85.213': 0.37; 'list.': 0.37; 'received:209': 0.38; 'to:addr:python.org': 0.40; 'ever': 0.60; 'back': 0.62; 'skip:n 10': 0.62; 'otten': 0.84; 'to:name:python': 0.84; 'average': 0.93
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=kj4QfMaWaMbxaSAtRdqApcMNDWZoarOFJkp1IOUSQ60=; b=tlRExTvC+DtE96WsLZXyDD1mIsSoAuEFNIYekD8UHY3a7A+SQA/ZoPdLQo2WYo/rNX /qr+81pXD7E4xTCGwbgTn1RLOY8xe33lJ0FsTxRGDXwNnWLfrkNWlF0uj5WKuPF8ztP0 vXAzqsazEauq5c19cUw90csiQycjYYGtjgf3a5e8sY2Q30rEf1FRRftL5/kE0LiQDGfl 6LdCG0UK2WDB+6NJx1xPTy44p/3JAGNjCkMsPxeBAsdBOFfM6GlHVtuThKs71M1HJkqs Z/5AIOM5PXib7Gth/ytIrfM/bG/ByaEhCB+RY5OJlOqSg4Qavew57rK3HWNtST8IHpO0 yPQQ==
X-Received by 10.50.78.231 with SMTP id e7mr5240975igx.93.1448473515072; Wed, 25 Nov 2015 09:45:15 -0800 (PST)
In-Reply-To <n3418c$k41$1@ger.gmane.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:99467

Show key headers only | View raw


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

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


Thread

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

csiph-web