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


Groups > comp.lang.python > #99485

Re: list slice and generators

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Pavlos Parissis <pavlos.parissis@gmail.com>
Newsgroups comp.lang.python
Subject Re: list slice and generators
Date Wed, 25 Nov 2015 21:43:20 +0100
Lines 133
Message-ID <mailman.93.1448484205.20593.python-list@python.org> (permalink)
References <5654D8CE.2020105@gmail.com> <n3418c$k41$1@ger.gmane.org>
Mime-Version 1.0
Content-Type multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="b85DCoNRWOEHfbkosRDRc50Uf8cvWFAPg"
X-Trace news.uni-berlin.de C0m+XY/zoP4esqellgMv9gE+jOecNbvp6iNiYqYikkAA==
Return-Path <pavlos.parissis@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'executed': 0.07; 'none)': 0.07; 'true)': 0.07; "'0',": 0.09; '[1,': 0.09; '[1]:': 0.09; '[2]:': 0.09; '[3]:': 0.09; 'false)': 0.09; 'generators': 0.09; 'metrics': 0.09; 'bug': 0.10; 'intermediate': 0.15; '(false,': 0.16; '(true,': 0.16; '>1)': 0.16; '[2,': 0.16; '[4]:': 0.16; 'assignment.': 0.16; 'bind': 0.16; 'filename:fname piece:signature': 0.16; 'generators.': 0.16; 'none]': 0.16; 'received:192.168.0.103': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'twice.': 0.16; 'variable.': 0.16; 'wrote:': 0.16; 'try:': 0.18; '(the': 0.22; 'cheers,': 0.22; 'bit': 0.23; 'elements': 0.23; 'header:In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; 'subject:list': 0.26; 'appreciated.': 0.27; 'least': 0.27; 'skip:( 20': 0.28; 'python2.7': 0.29; 'thus,': 0.29; 'skip:[ 10': 0.31; 'problem': 0.33; 'avoiding': 0.33; 'message-id:@gmail.com': 0.34; 'except': 0.34; 'list': 0.34; 'received:google.com': 0.35; 'could': 0.35; 'question,': 0.35; 'replace': 0.35; 'received:74.125.82': 0.35; "isn't": 0.35; 'item': 0.35; 'skip:i 20': 0.36; 'should': 0.36; 'there': 0.36; 'possible': 0.36; 'evaluation': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'thanks': 0.37; 'list.': 0.37; 'doing': 0.38; 'hi,': 0.38; 'received:192': 0.39; 'to:addr:python.org': 0.40; 'your': 0.60; 'avoid': 0.61; 'side': 0.62; 'back': 0.62; 'skip:n 10': 0.62; 'goal': 0.64; 'dangerous': 0.70; '11:07': 0.84; 'otten': 0.84; 'way)': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type; bh=Un23aJ+yQx5V2GWBMPIVCcyRXU9mIqbDMx+lNYg/qhk=; b=jzeDbLPCwqzhI5oi9/oMVgUhyaJnO7oUk0OoHGIsrw2Mx7XlLTEG6NqOh3dpisYqU/ MK1HoyzfxEmKUDniUXIH0TecbiSCkz9cDnO4ucSF2+p/JNJfw4HTxm+Ld9+HgJId6CeW HyNJF8VjW5UB1WUl2WvLW42vDYIHfA6jIiZQHq8cNtCFQz8I6adqGvhkFFOEpx+2Kewh uIaZPXiERheaunvA57GTc3o2jAhUsewIG+VAnoOA38vLELSzR+IykvB8CKwAWlhNHrTd 6W8o7Mk/5VXLLmhhmmghIPdR4fG6znQ6Zm48Hj3zrVFaYUGCBgUjSYhG8b45ujhT+Hh9 un7A==
X-Received by 10.28.48.10 with SMTP id w10mr6469417wmw.39.1448484203776; Wed, 25 Nov 2015 12:43:23 -0800 (PST)
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Icedove/31.8.0
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:99485

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

On 25/11/2015 11:07 πμ, Peter Otten wrote:
> Pavlos Parissis wrote:
> 
>> Hi,
>>
>> Do you see any possible dangerous hidden bug in the below code(using
>> python2.7 and python3.4)?
>>
>> My goal is to avoid go through the metrics list twice. But, I don't
>> know if there will be a problem with doing in place replace of list
>> elements using 2 generators.
>>
>> # metrics = ['', '0', '10'....]
>> metrics = [x.metric(name) for x in self._server_per_proc]
>> metrics[:] = (converter(x) for x in metrics)
>> metrics[:] = (x for x in metrics if x is not None)
> 
> Both generators are executed immediately, and the right side is always 
> evaluated before the the slice assignment. Try
> 

This is what I was vaguely remembering, thanks for the confirmation.

> metrics = (x.metric(name) for x in self._server_per_proc)
> metrics = (converter(x) for x in metrics)
> metrics = [x for x in metrics if x is not None]
> 

I see you prefer to bind the result of the evaluation to the same
variable. I learned (the hard way) that it could lead to problems where::

In [1]: a = [1, 2, 3]

In [2]: b = a

In [3]: a == b, a is b
Out[3]: (True, True)

In [4]: a = (x for x in a if x >1)

In [5]: a == b, a is b
Out[5]: (False, False)

In [6]: a
Out[6]: <generator object <genexpr> at 0x7f7f8b7400d8>

In [7]: list(a)
Out[7]: [2, 3]

In [8]: a = [1, 2, 3]

In [9]: b = a

In [10]: a == b, a is b
Out[10]: (True, True)

In [11]: a = [x for x in a if x >1]

In [12]: a == b, a is b
Out[12]: (False, False)

In [13]: a
Out[13]: [2, 3]

In [14]: b
Out[14]: [1, 2, 3]

Thus, I always use slice assignment, even when above case isn't applied.

> or
> 
> metrics = (converter(x.metric(name)) for x in self._server_per_proc)
> metrics = [x for x in metrics if x is not None]
> 

I should do the above.

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

Tricky, indeed

Thanks for your time and effort to answer my question, it is very
much appreciated.

Cheers,
Pavlos

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


Thread

Re: list slice and generators Pavlos Parissis <pavlos.parissis@gmail.com> - 2015-11-25 21:43 +0100

csiph-web