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


Groups > comp.lang.python > #99968

Re: filter a list of strings

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Terry Reedy <tjreedy@udel.edu>
Newsgroups comp.lang.python
Subject Re: filter a list of strings
Date Thu, 3 Dec 2015 11:19:29 -0500
Lines 46
Message-ID <mailman.176.1449159615.14615.python-list@python.org> (permalink)
References <3p9zlt5t5Vz5vN5@dovecot03.posteo.de> <n3pch1$igb$1@ger.gmane.org>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding 7bit
X-Trace news.uni-berlin.de ut01L1SHMstWFpWM/RUCdg646Pz2i1gn2c2ewKvXoXcg==
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.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'one?': 0.05; 'filter,': 0.09; '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; 'jan': 0.11; '10.000': 0.16; 'boolean': 0.16; 'btw:': 0.16; 'deleting,': 0.16; 'item:': 0.16; 'len': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'reedy': 0.16; 'targets:': 0.16; 'wrote:': 0.16; 'string': 0.17; 'lawrence': 0.22; 'am,': 0.23; '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; 'list': 0.34; 'could': 0.35; 'done': 0.35; 'filter': 0.35; 'replace': 0.35; 'item': 0.35; 'instead': 0.36; 'there': 0.36; 'received:71': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'copying': 0.38; 'to:addr:python.org': 0.40; 'where': 0.40; 'mark': 0.40; 'more': 0.63; 'course.': 0.67; '.....': 0.76; '100': 0.79; 'absolutely': 0.88; 'received:fios.verizon.net': 0.91
X-Injected-Via-Gmane http://gmane.org/
X-Gmane-NNTP-Posting-Host pool-71-185-227-36.phlapa.fios.verizon.net
User-Agent Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.4.0
In-Reply-To <n3pch1$igb$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:99968

Show key headers only | View raw


On 12/3/2015 7:28 AM, Mark Lawrence wrote:
> 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 :)

Even better, instead of copying and deleting, which is O(k*n), where n 
is the len of list and k is number deletions, is to create a new list 
with the item you want.  In other words, actually filter, as you said 
you want.

targets = {'Banana', 'Car', ...}  # set intentional
newlist = [item for item in oldlist if item not in targets]

Generically, replace 'not in targets' with any boolean expression or 
function.


-- 
Terry Jan Reedy

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


Thread

Re: filter a list of strings Terry Reedy <tjreedy@udel.edu> - 2015-12-03 11:19 -0500

csiph-web