Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #99954
| Path | csiph.com!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail |
|---|---|
| From | Grobu <snailcoder@retrosite.invalid> |
| Newsgroups | comp.lang.python |
| Subject | Re: filter a list of strings |
| Date | Thu, 03 Dec 2015 13:17:30 +0100 |
| Organization | A noiseless patient Spider |
| Lines | 35 |
| Message-ID | <n3pbns$i4e$1@dont-email.me> (permalink) |
| References | <mailman.155.1449122975.14615.python-list@python.org> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=windows-1252; format=flowed |
| Content-Transfer-Encoding | 7bit |
| Injection-Date | Thu, 3 Dec 2015 12:14:52 -0000 (UTC) |
| Injection-Info | mx02.eternal-september.org; posting-host="9a195c273c7358eff14804b45945f6c4"; logging-data="18574"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX181kGXepP9MG1/hfhQAxrk9" |
| User-Agent | Mozilla/5.0 (X11; Linux i686; rv:31.0) Gecko/20100101 Icedove/31.8.0 |
| In-Reply-To | <mailman.155.1449122975.14615.python-list@python.org> |
| Cancel-Lock | sha1:uzNPwmEeXajz818nzuUonoLfGag= |
| Xref | csiph.com comp.lang.python:99954 |
Show key headers only | View raw
On 03/12/15 02: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. > > btw: Is it correct to iterate over a copy (list[:]) of that string list > and not the original one? > No idea how 'Pythonic' this would be considered, but you could use a combination of filter() with a regular expression : # ------------------------------------------------------------------ import re list = ... pattern = re.compile( r'banana|car', re.I ) filtered_list = filter( lambda line: not pattern.search(line), list ) # ------------------------------------------------------------------ HTH
Back to comp.lang.python | Previous | Next — Previous 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