Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #99955 > unrolled thread
| Started by | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| First post | 2015-12-03 12:28 +0000 |
| Last post | 2015-12-09 16:35 +0200 |
| Articles | 4 — 4 participants |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
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
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2015-12-03 12:28 +0000 |
| Subject | Re: filter a list of strings |
| Message-ID | <mailman.171.1449145727.14615.python-list@python.org> |
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
[toc] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2015-12-08 23:58 +0100 |
| Message-ID | <2598115.t018ozAsXX@PointedEars.de> |
| In reply to | #99955 |
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 = ..... >> […] > > 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 :) However, “list” is a built-in class/constructor that would be overwritten this way. One should choose another identifier than “list” for one’s variables. -- PointedEars Twitter: @PointedEars2 Please do not cc me. / Bitte keine Kopien per E-Mail.
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2015-12-09 18:38 +1100 |
| Subject | Shadowing built-ins [was Re: filter a list of strings] |
| Message-ID | <5667da63$0$14486$c3e8da3@news.astraweb.com> |
| In reply to | #100178 |
On Wednesday 09 December 2015 09:58, Thomas 'PointedEars' Lahn wrote:
> 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 = .....
>>> […]
[...]
> However, “list” is a built-in class/constructor that would be overwritten
> this way. One should choose another identifier than “list” for one’s
> variables.
Correct, well mostly correct.
The original built-in list is *not* overridden, as such, but shadowed: it
still exists, but access to it is blocked by the local variable "list". The
list *type* still exists, only the *name* list is shadowed. So you can
easily recover from accidentally shadowing the name by deleting the local:
del list
To beginners, accidental shadowing of built-ins is a serious source of
confusion. But to experts, intentional shadowing can be useful.
There's nothing wrong with (say):
def sort_and_pop(list):
list.sort()
list.pop(0)
Yes, the local "list" shadows the built-in list. So what?
More usefully, one can monkey-patch a module by shadowing a built-in in that
module:
# Python 3 only.
import builtins
def print(*args, **kwargs):
log_something()
builtins.print(*args, **kwargs)
--
Steve
[toc] | [prev] | [next] | [standalone]
| From | Sivan Greenberg <sivan@vitakka.co> |
|---|---|
| Date | 2015-12-09 16:35 +0200 |
| Message-ID | <mailman.94.1449671840.12405.python-list@python.org> |
| In reply to | #100178 |
That might also work: new_list = [i for i in the_list if i not in targets] # given you have no special requirements for the selection # out of 'targets' -Sivan On Wed, Dec 9, 2015 at 12:58 AM, Thomas 'PointedEars' Lahn < PointedEars@web.de> wrote: > 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 = ..... > >> […] > > > > 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 :) > > However, “list” is a built-in class/constructor that would be overwritten > this way. One should choose another identifier than “list” for one’s > variables. > > -- > PointedEars > > Twitter: @PointedEars2 > Please do not cc me. / Bitte keine Kopien per E-Mail. > -- > https://mail.python.org/mailman/listinfo/python-list > -- Sivan Greenberg Co founder & CTO Vitakka Consulting
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web