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


Groups > comp.lang.python > #99955

Re: filter a list of strings

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Mark Lawrence <breamoreboy@yahoo.co.uk>
Newsgroups comp.lang.python
Subject Re: filter a list of strings
Date Thu, 3 Dec 2015 12:28:07 +0000
Lines 36
Message-ID <mailman.171.1449145727.14615.python-list@python.org> (permalink)
References <3p9zlt5t5Vz5vN5@dovecot03.posteo.de>
Mime-Version 1.0
Content-Type text/plain; charset=windows-1252; format=flowed
Content-Transfer-Encoding 7bit
X-Trace news.uni-berlin.de kscaqfFwEgJIFOY+P859kgmvUO2qKJ1s0rrMYcM2olLA==
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'from:addr:yahoo.co.uk': 0.05; 'one?': 0.05; 'items)': 0.09; 'iterate': 0.09; 'long)': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'targets': 0.09; '10.000': 0.16; 'btw:': 0.16; 'item:': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'targets:': 0.16; 'wrote:': 0.16; 'string': 0.17; 'language': 0.19; 'lawrence': 0.22; 'code.': 0.23; 'header:In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; "doesn't": 0.26; 'example': 0.26; 'subject:list': 0.26; 'header:X-Complaints-To:1': 0.26; 'correct': 0.28; 'language.': 0.32; 'list': 0.34; 'could': 0.35; 'done': 0.35; 'filter': 0.35; 'item': 0.35; 'there': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'to:addr:python.org': 0.40; 'mark': 0.40; 'charset:windows-1252': 0.62; 'more': 0.63; 'our': 0.64; 'course.': 0.67; '.....': 0.76; '100': 0.79; 'pythonistas,': 0.84; 'absolutely': 0.88
X-Injected-Via-Gmane http://gmane.org/
X-Gmane-NNTP-Posting-Host 195.147.236.181
User-Agent Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.4.0
In-Reply-To <3p9zlt5t5Vz5vN5@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:99955

Show key headers only | View raw


On 03/12/2015 01:15, c.buhtz@posteo.jp wrote:
> I would like to know how this could be done more elegant/pythonic.
>
> I have a big list (over 10.000 items) with strings (each 100 to 300
> chars long) and want to filter them.
>
> list = .....
>
> for item in list[:]:
>    if 'Banana' in item:
>       list.remove(item)
>    if 'Car' in item:
>       list.remove(item)
>
> There are a lot of more conditions of course. This is just example code.
> It doesn't look nice to me. To much redundance.

targets = ['Banana', 'Car'...]
for item in list[:]:
     for target in targets:
         if target in item:
             list.remove(item)

>
> btw: Is it correct to iterate over a copy (list[:]) of that string list
> and not the original one?
>

Absolutely :)

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

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


Thread

Re: filter a list of strings Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-12-03 12:28 +0000
  Re: filter a list of strings Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-12-08 23:58 +0100
    Shadowing built-ins [was Re: filter a list of strings] Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-12-09 18:38 +1100
    Re: filter a list of strings Sivan Greenberg <sivan@vitakka.co> - 2015-12-09 16:35 +0200

csiph-web