Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #99947
| Path | csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | Chris Angelico <rosuav@gmail.com> |
| Newsgroups | comp.lang.python |
| Subject | Re: filter a list of strings |
| Date | Thu, 3 Dec 2015 20:40:51 +1100 |
| Lines | 38 |
| Message-ID | <mailman.166.1449135660.14615.python-list@python.org> (permalink) |
| References | <mailman.155.1449122975.14615.python-list@python.org> <lf51tb459f2.fsf@ling.helsinki.fi> <3pBBdn3nVcz5vP8@dovecot03.posteo.de> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=UTF-8 |
| X-Trace | news.uni-berlin.de Y2BvIYdZI/rtJP93H1z60QfkUESNUlSBy/OXqwCUX/Ig== |
| Return-Path | <rosuav@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.012 |
| X-Spam-Evidence | '*H*': 0.98; '*S*': 0.00; 'that?': 0.05; 'cc:addr :python-list': 0.09; 'happen.': 0.09; 'python': 0.10; 'thu,': 0.15; '(before': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'suggestion.': 0.16; 'wrote:': 0.16; 'basically': 0.18; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'posted': 0.21; 'fairly': 0.22; 'bit': 0.23; 'dec': 0.23; 'this:': 0.23; 'header :In-Reply-To:1': 0.24; 'subject:list': 0.26; 'equivalent': 0.27; 'message-id:@mail.gmail.com': 0.27; 'lot.': 0.29; 'code': 0.30; 'call,': 0.33; 'combination': 0.33; 'know.': 0.34; 'list': 0.34; 'received:google.com': 0.35; 'list:': 0.35; 'item': 0.35; 'but': 0.36; 'there': 0.36; 'received:209.85': 0.36; 'beginning': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'received:209.85.213': 0.37; 'front': 0.38; 'received:209': 0.38; 'thank': 0.38; 'goes': 0.39; 'well.': 0.40; 'called': 0.40; 'your': 0.60; 'saw': 0.77; 'chrisa': 0.84; 'post,': 0.84; 'to:none': 0.91; 'imagine': 0.96 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=hOpl0+MFb4kp3YRAPLExlW+8NucQsBsa9ABrGZPuduo=; b=CSdVWYz1hKee1hScKTJzsMSnnHL5st7bJG4G0V4Ehnu8I2aN1vTPWuWSRhyNwsLxz2 ka2ybXArTN+66nhb0ZS14R66UDsEXsjpJotIrdJPWVIE4X3d4Bj4WH2HAZk85S6eWiy0 uaTqbYYE+W6afGwxQLsNxySfRy4JCCQJfu6FpPt2eB9yacoBeyoIAK9NtLpuWXWqp0US 1daD65jPvG0vsAQEfxr80ih0tqlx1BzCi5foWcLNd+JaD0j6p6ZzOC3KZ+GKCC1CdTbz IrHRyC9ue7MEwGLCNcq1Hn8Piv3mCxgaGUe0gGJOQPAWn8HddUpQM3gGd7/JQiw+QSOv /5iw== |
| X-Received | by 10.50.30.6 with SMTP id o6mr39107397igh.94.1449135651480; Thu, 03 Dec 2015 01:40:51 -0800 (PST) |
| In-Reply-To | <3pBBdn3nVcz5vP8@dovecot03.posteo.de> |
| 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:99947 |
Show key headers only | View raw
On Thu, Dec 3, 2015 at 8:27 PM, <c.buhtz@posteo.jp> wrote:
> Thank you for your suggestion. This will help a lot.
>
> On 2015-12-03 08:32 Jussi Piitulainen <harvesting@is.invalid> wrote:
>> list = [ item for item in list
>> if ( 'Banana' not in item and
>> 'Car' not in item ) ]
>
> I often saw constructions like this
> x for x in y if ...
> But I don't understand that combination of the Python keywords (for,
> in, if) I allready know. It is to complex to imagine what there really
> happen.
>
> I understand this
> for x in y:
> if ...
>
> But what is about the 'x' in front of all that?
It's called a *list comprehension*. The code Jussi posted is broadly
equivalent to this:
list = []
for item in list:
if ( 'Banana' not in item and
'Car' not in item ):
list.append(item)
I recently came across this blog post, which visualizes comprehensions
fairly well.
http://treyhunner.com/2015/12/python-list-comprehensions-now-in-color/
The bit at the beginning (before the first 'for') goes inside a
list.append(...) call, and then everything else is basically the same.
ChrisA
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
filter a list of strings <c.buhtz@posteo.jp> - 2015-12-03 02:15 +0100
Re: filter a list of strings Jussi Piitulainen <harvesting@is.invalid> - 2015-12-03 08:32 +0200
Re: filter a list of strings <c.buhtz@posteo.jp> - 2015-12-03 10:27 +0100
Re: filter a list of strings Jussi Piitulainen <harvesting@is.invalid> - 2015-12-03 13:53 +0200
Re: filter a list of strings Peter Pearson <pkpearson@nowhere.invalid> - 2015-12-05 19:42 +0000
Re: filter a list of strings Chris Angelico <rosuav@gmail.com> - 2015-12-03 20:40 +1100
Re: filter a list of strings Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> - 2015-12-03 10:46 +0100
Re: filter a list of strings Laura Creighton <lac@openend.se> - 2015-12-03 10:53 +0100
Re: filter a list of strings jmp <jeanmichel@sequans.com> - 2015-12-03 11:03 +0100
Re: filter a list of strings Peter Otten <__peter__@web.de> - 2015-12-03 11:13 +0100
Re: filter a list of strings Denis McMahon <denismfmcmahon@gmail.com> - 2015-12-03 14:16 +0000
Re: filter a list of strings Jussi Piitulainen <harvesting@is.invalid> - 2015-12-03 17:02 +0200
Re: filter a list of strings Grobu <snailcoder@retrosite.invalid> - 2015-12-03 13:17 +0100
csiph-web