Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Pavlos Parissis Newsgroups: comp.lang.python Subject: Re: list slice and generators Date: Wed, 25 Nov 2015 21:43:20 +0100 Lines: 133 Message-ID: References: <5654D8CE.2020105@gmail.com> 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: 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: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:99485 This is an OpenPGP/MIME signed message (RFC 4880 and 3156) --b85DCoNRWOEHfbkosRDRc50Uf8cvWFAPg Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable On 25/11/2015 11:07 =CF=80=CE=BC, Peter Otten wrote: > Pavlos Parissis wrote: >=20 >> 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 =3D ['', '0', '10'....] >> metrics =3D [x.metric(name) for x in self._server_per_proc] >> metrics[:] =3D (converter(x) for x in metrics) >> metrics[:] =3D (x for x in metrics if x is not None) >=20 > Both generators are executed immediately, and the right side is always = > evaluated before the the slice assignment. Try >=20 This is what I was vaguely remembering, thanks for the confirmation. > metrics =3D (x.metric(name) for x in self._server_per_proc) > metrics =3D (converter(x) for x in metrics) > metrics =3D [x for x in metrics if x is not None] >=20 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 =3D [1, 2, 3] In [2]: b =3D a In [3]: a =3D=3D b, a is b Out[3]: (True, True) In [4]: a =3D (x for x in a if x >1) In [5]: a =3D=3D b, a is b Out[5]: (False, False) In [6]: a Out[6]: at 0x7f7f8b7400d8> In [7]: list(a) Out[7]: [2, 3] In [8]: a =3D [1, 2, 3] In [9]: b =3D a In [10]: a =3D=3D b, a is b Out[10]: (True, True) In [11]: a =3D [x for x in a if x >1] In [12]: a =3D=3D 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 >=20 > metrics =3D (converter(x.metric(name)) for x in self._server_per_proc) > metrics =3D [x for x in metrics if x is not None] >=20 I should do the above. > to get down to one intermediate list. Avoiding the last one is a bit tr= icky: >=20 > metrics =3D (converter(x.metric(name)) for x in self._server_per_proc) > metrics =3D (x for x in metrics if x is not None) > try: > # if there is at least one item the generator is not empty > first =3D next(metrics) > except StopIteration: > metrics =3D () > else: > # put the first item back in > metrics =3D itertools.chain([first], metrics) > assert metrics >=20 Tricky, indeed Thanks for your time and effort to answer my question, it is very much appreciated. Cheers, Pavlos --b85DCoNRWOEHfbkosRDRc50Uf8cvWFAPg Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v2 iQIcBAEBCAAGBQJWVh1oAAoJEIP8ktofcXa5GBAP/jI4fZEU/Ydl/t1Hz5mluLKW RNLeRZ8qMeB66iZu6np8oEhXV1UOvzMzte7fUSY2H1ZNLCDtGLIqNLJALs1h4vmZ 8DgwIRiydzSNG0kcfNZl6ci8ul13062ii0anViVvFL2PcIoF2MsHtYNRPN8vAMZ+ hHGTlYO5rgx86YUTmsvj5/utOss+iD21dfBY6vxo3wQbZLhr6n+DDcIcf+y9d/Sw Mj8OztNYDkRIR8z6nicUKgtFnbWvgcUMQr0yEX79MkXPM2jHYN45XerX8BxqqDrL mWwBpghq5EkY3nnV7rHP7n/TIBtgytlSTxDaSduWC8lvyLnk9mO2P0x9JCQcIW0h qyrCsq5GbuR97JmwcH4PCkp1k73Iu6zXlU5Bjme3WdqySNaqE8jI2EWzc00czPMz snukP3hhtbQF4CF9RJ444ukdiI+/rFUGzgkgUTW+ntyiUMpj/EgFwnK7kBAlrVRn NnXLFyCXlDw52Jro4VQQnsty+CqFT9LRdU3xZ4mEcZGijO8zrJE/nRzJ4kAwa0P1 LGCUjQCphCZ4tGSTSn09E25yUz/PXv/0rzxGdCozSxTvsRD+eQ3d/5mQrbI6yPcI 4ONIwzgaarqkZBNqVYQBlDfyIxcC7bb1xFX8I0sDFguVrDCggYXgmFAmQ9KrtPix zFvJQmpUAoJ3uj6fFU62 =LhYB -----END PGP SIGNATURE----- --b85DCoNRWOEHfbkosRDRc50Uf8cvWFAPg--