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


Groups > comp.lang.python > #99949

Re: filter a list of strings

Path csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From Laura Creighton <lac@openend.se>
Newsgroups comp.lang.python
Subject Re: filter a list of strings
Date Thu, 03 Dec 2015 10:53:16 +0100
Lines 34
Message-ID <mailman.168.1449136443.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="us-ascii"
Content-Transfer-Encoding quoted-printable
X-Trace news.uni-berlin.de y0wjn/1xfxAi39zE/oN7Zg4e0z4AZpg0hbLMSHv0bizQ==
Return-Path <lac@openend.se>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'that?': 0.05; 'cc:addr :python-list': 0.09; 'received:openend.se': 0.09; 'received:theraft.openend.se': 0.09; 'python': 0.10; 'thu,': 0.15; '>on': 0.16; 'cc:addr:lac': 0.16; 'cc:addr:openend.se': 0.16; 'from:addr:lac': 0.16; 'from:addr:openend.se': 0.16; 'from:name:laura creighton': 0.16; 'message-id:@fido.openend.se': 0.16; 'received:fido': 0.16; 'received:fido.openend.se': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'suggestion.': 0.16; 'wrote:': 0.16; 'laura': 0.18; 'skip:l 30': 0.18; '2015': 0.20; 'cc:addr:python.org': 0.20; 'cc:2**1': 0.22; 'see:': 0.22; 'dec': 0.23; 'this:': 0.23; 'subject:list': 0.26; 'lot.': 0.29; 'received:se': 0.29; 'cc:no real name:2**1': 0.29; 'problem': 0.33; 'url:python': 0.33; 'combination': 0.33; 'know.': 0.34; 'list': 0.34; 'item': 0.35; 'but': 0.36; 'there': 0.36; 'url:org': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'charset:us-ascii': 0.37; 'front': 0.38; 'your': 0.60; 'header:Message-Id:1': 0.61; 'saw': 0.77; 'header:In-reply-to:1': 0.84; 'url:tutorial': 0.91; 'imagine': 0.96
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=openend.se; s=default; t=1449136402; bh=HDQk6WDyyi8HYVZr2fuNlOxRRdym5fSBGflaWxfCdgs=; h=To:cc:From:Subject:In-reply-to:References:Date:From; b=eqM0c0gpA/TM0N0FhuHp8kjoa3u4AclFB1CoczqduOijJzrO3gW30cb/xNM9wW9Fm +3nWkwMAJzH9++S5baAvIBIij/K785hDAR2TcYGYSFGaqJckDC48hdEHkTq2SgR+SX Ne9B3PXn37vH0FyHn3hov7EhP7Zt2GqQznEMy1OQ=
In-reply-to <3pBBdn3nVcz5vP8@dovecot03.posteo.de>
Comments In-reply-to <c.buhtz@posteo.jp> message dated "Thu, 03 Dec 2015 10:27:19 +0100."
Content-ID <11530.1449136396.1@fido>
X-Greylist Sender IP whitelisted, not delayed by milter-greylist-4.3.9 (theraft.openend.se [82.96.5.2]); Thu, 03 Dec 2015 10:53:22 +0100 (CET)
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:99949

Show key headers only | View raw


In a message of Thu, 03 Dec 2015 10:27:19 +0100, c.buhtz@posteo.jp writes:
>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?

This is a list comprehension.
see: https://docs.python.org/2/tutorial/datastructures.html#list-comprehensions

But I would solve your problem like this:

things_I_do_not_want = ['Car', 'Banana', <add all of them here>]
things_I_want = []

for item in list_of_everything_I_started_with:
    if item not in things_I_do_not_want:
       things_I_want.append(item)

Laura

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


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