Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > What is the best way to delete strings in a string list that thatmatch certain pattern?

Reply
Thread Tools

What is the best way to delete strings in a string list that thatmatch certain pattern?

 
 
Peng Yu
Guest
Posts: n/a
 
      11-07-2009
On Sat, Nov 7, 2009 at 8:54 AM, Steven D'Aprano
<(E-Mail Removed)> wrote:
> On Fri, 06 Nov 2009 10:16:58 -0600, Peng Yu wrote:
>
>> What is a list-comprehension?

>
> Time for you to Read The Fine Manual.
>
> http://docs.python.org/tutorial/index.html
>
>
>> I tried the following code. The list 'l' will be ['a','b','c'] rather
>> than ['b','c'], which is what I want. It seems 'remove' will disrupt the
>> iterator, right? I am wondering how to make the code correct.
>>
>> l = ['a', 'a', 'b', 'c']
>> for x in l:
>> * if x == 'a':
>> * * l.remove(x)

>
>
> Oh lordy, it's Shlemiel the Painter's algorithm. Please don't do that for
> lists with more than a handful of items. Better still, please don't do
> that.
>
> http://www.joelonsoftware.com/articl...000000319.html


I understand what is Shlemiel the Painter's algorithm. But if the
iterator can be intelligently adjusted in my code upon 'remove()', is
my code Shlemiel the Painter's algorithm?
 
Reply With Quote
 
 
 
 
Peng Yu
Guest
Posts: n/a
 
      11-07-2009
On Fri, Nov 6, 2009 at 5:57 PM, Dave Angel <(E-Mail Removed)> wrote:
>
>
> Peng Yu wrote:
>>
>> On Fri, Nov 6, 2009 at 10:42 AM, Robert P. J. Day <(E-Mail Removed)>
>> wrote:
>>
>>>
>>> On Fri, 6 Nov 2009, Peng Yu wrote:
>>>
>>>
>>>>
>>>> On Fri, Nov 6, 2009 at 3:05 AM, Diez B. Roggisch <(E-Mail Removed)>
>>>> wrote:
>>>>
>>>>>
>>>>> Peng Yu schrieb:
>>>>>
>>>>>>
>>>>>> Suppose I have a list of strings, A. I want to compute the list (call
>>>>>> it B) of strings that are elements of A but doesn't match a regex. I
>>>>>> could use a for loop to do so. In a functional language, there is way
>>>>>> to do so without using the for loop.
>>>>>>
>>>>>
>>>>> Nonsense. For processing over each element, you have to loop over them,
>>>>> either with or without growing a call-stack at the same time.
>>>>>
>>>>> FP languages can optimize away the stack-frame-growth (tail recursion)
>>>>> - but
>>>>> this isn't reducing complexity in any way.
>>>>>
>>>>> So use a loop, either directly, or using a list-comprehension.
>>>>>
>>>>
>>>> What is a list-comprehension?
>>>>
>>>> I tried the following code. The list 'l' will be ['a','b','c'] rather
>>>> than ['b','c'], which is what I want. It seems 'remove' will disrupt
>>>> the iterator, right? I am wondering how to make the code correct.
>>>>
>>>> l ='a', 'a', 'b', 'c']
>>>> for x in l:
>>>> *if x ='a':
>>>> * *l.remove(x)
>>>>
>>>> print l
>>>>
>>>
>>> *list comprehension seems to be what you want:
>>>
>>> *l =i for i in l if i != 'a']
>>>

>>
>> My problem comes from the context of using os.walk(). Please see the
>> description of the following webpage. Somehow I have to modify the
>> list inplace. I have already tried 'dirs =i for i in l if dirs !'a']'. But
>> it seems that it doesn't "prune the search". So I need the
>> inplace modification of list.
>>
>> http://docs.python.org/library/os.html
>>
>> When topdown is True, the caller can modify the dirnames list in-place
>> (perhaps using del or slice assignment), and walk() will only recurse
>> into the subdirectories whose names remain in dirnames; this can be
>> used to prune the search, impose a specific order of visiting, or even
>> to inform walk() about directories the caller creates or renames
>> before it resumes walk() again. Modifying dirnames when topdown is
>> False is ineffective, because in bottom-up mode the directories in
>> dirnames are generated before dirpath itself is generated.
>>
>>

>
> The context is quite important in this case. *The os.walk() iterator gives
> you a tuple of three values, and one of them is a list. *You do indeed want
> to modify that list, but you usually don't want to do it "in-place." * I'll
> show you the in-place version first, then show you the slice approach.
>
> If all you wanted to do was to remove one or two specific items from the
> list, then the remove method would be good. *So in your example, you don' t
> need a loop. *Just say:
> * if 'a' in dirs:
> * * * *dirs.remove('a')
>
> But if you have an expression you want to match each dir against, the list
> comprehension is the best answer. *And the trick to stuffing that new list
> into the original list object is to use slicing on the left side. *The [:]
> notation is a default slice that means the whole list.
>
> * dirs[:] = [ item for item in dirs if * * bool_expression_on_item ]


I suggest to add this example to the document of os.walk() to make
other users' life easier.
 
Reply With Quote
 
 
 
 
Robert P. J. Day
Guest
Posts: n/a
 
      11-07-2009
On Sat, 7 Nov 2009, Peng Yu wrote:

> On Fri, Nov 6, 2009 at 5:57 PM, Dave Angel <(E-Mail Removed)> wrote:


> > But if you have an expression you want to match each dir against,
> > the list comprehension is the best answer. *And the trick to
> > stuffing that new list into the original list object is to use
> > slicing on the left side. *The [:] notation is a default slice
> > that means the whole list.
> >
> > * dirs[:] = [ item for item in dirs if * * bool_expression_on_item ]

>
> I suggest to add this example to the document of os.walk() to make
> other users' life easier.


huh? why do you need the slice notation on the left? why can't you
just assign to "dirs" as opposed to "dirs[:]"? using the former seems
to work just fine. is this some kind of python optimization or idiom?

rday
--


================================================== ======================
Robert P. J. Day Waterloo, Ontario, CANADA

Linux Consulting, Training and Kernel Pedantry.

Web page: http://crashcourse.ca
Twitter: http://twitter.com/rpjday
================================================== ======================
 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      11-07-2009
Robert P. J. Day wrote:

> On Sat, 7 Nov 2009, Peng Yu wrote:
>
>> On Fri, Nov 6, 2009 at 5:57 PM, Dave Angel <(E-Mail Removed)> wrote:

>
>> > But if you have an expression you want to match each dir against,
>> > the list comprehension is the best answer. And the trick to
>> > stuffing that new list into the original list object is to use
>> > slicing on the left side. The [:] notation is a default slice
>> > that means the whole list.
>> >
>> > dirs[:] = [ item for item in dirs if bool_expression_on_item ]

>>
>> I suggest to add this example to the document of os.walk() to make
>> other users' life easier.

>
> huh? why do you need the slice notation on the left? why can't you
> just assign to "dirs" as opposed to "dirs[:]"? using the former seems
> to work just fine. is this some kind of python optimization or idiom?


dirs = [...]

rebinds the name "dirs" while

dirs[:] = [...]

updates the contents of the list currently bound to the "dirs" name. The
latter is necessary in the context of os.walk() because it yields a list of
subdirectories, gives the user a chance to update it and than uses this
potentially updated list to decide which subdirectories to descend into.
A simplified example:

>>> def f():

.... items = ["a", "b", "c"]
.... yield items
.... print items
....
>>> for items in f():

.... items = ["x", "y"]
....
['a', 'b', 'c']
>>> for items in f():

.... items[:] = ["x", "y"]
....
['x', 'y']

Peter

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
Best way to delete contents of a list list of (pointers) Angus C++ 6 01-23-2010 01:05 PM
best efficient and readable way to concatenate strings (or the best trade-offs) Diego Martins C++ 5 06-19-2007 02:27 PM
Strings, Strings and Damned Strings Ben C Programming 14 06-24-2006 05:09 AM
convert list of strings to set of regexes; convert list of strings to trie Klaus Neuner Python 7 07-26-2004 07:25 AM



Advertisments