Go Back   Velocity Reviews > Newsgroups > Python
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

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

 
Thread Tools Search this Thread
Old 11-06-2009, 04:19 AM   #1
Default What is the best way to delete strings in a string list that thatmatch certain pattern?


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.

I'm wondering what is the best way to compute B in python.


Peng Yu
  Reply With Quote
Old 11-06-2009, 07:30 AM   #2
Lie Ryan
 
Posts: n/a
Default Re: What is the best way to delete strings in a string list thatthat match certain pattern?
Peng Yu wrote:
> 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.


In functional language, there is no looping, so that argument is kind of
pointless. The looping construct in many functional language is a syntax
sugar for recursion.

In python, instead of explicit loop, you can use either:
map(pattern.match, list_of_strs)
or
[pattern.match(mystr) for mystr in list_of_strs]

or if you want to be wicked evil, you can write a recursive function as
such:

def multimatcher(list_of_strs, index=0):
return [] if index >= len(list_of_strs) else (
multimatcher(
list_of_strs[index + 1]
).append(
pattern.match(list_of_strs[index])
)
)


Lie Ryan
  Reply With Quote
Old 11-06-2009, 09:05 AM   #3
Diez B. Roggisch
 
Posts: n/a
Default Re: What is the best way to delete strings in a string list thatthat match certain pattern?
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.

Diez


Diez B. Roggisch
  Reply With Quote
Old 11-06-2009, 04:16 PM   #4
Peng Yu
 
Posts: n/a
Default Re: What is the best way to delete strings in a string list that thatmatch certain pattern?
On Fri, Nov 6, 2009 at 3:05 AM, Diez B. Roggisch <> 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


Peng Yu
  Reply With Quote
Old 11-06-2009, 04:42 PM   #5
Robert P. J. Day
 
Posts: n/a
Default Re: What is the best way to delete strings in a string list thatthat match certain pattern?
On Fri, 6 Nov 2009, Peng Yu wrote:

> On Fri, Nov 6, 2009 at 3:05 AM, Diez B. Roggisch <> 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']

rday
--


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

Linux Consulting, Training and Kernel Pedantry.

Web page: http://crashcourse.ca
Twitter: http://twitter.com/rpjday
================================================== ======================


Robert P. J. Day
  Reply With Quote
Old 11-06-2009, 04:58 PM   #6
Peng Yu
 
Posts: n/a
Default Re: What is the best way to delete strings in a string list that thatmatch certain pattern?
On Fri, Nov 6, 2009 at 10:42 AM, Robert P. J. Day <> wrote:
> On Fri, 6 Nov 2009, Peng Yu wrote:
>
>> On Fri, Nov 6, 2009 at 3:05 AM, Diez B. Roggisch <> 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.


Peng Yu
  Reply With Quote
Old 11-06-2009, 05:05 PM   #7
Peter Otten
 
Posts: n/a
Default Re: What is the best way to delete strings in a string list that that match certain pattern?
Peng Yu wrote:

> 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.


Use

dirs[:] = [d for d in dirs if d != "a"]

or

try:
dirs.remove("a")
except ValueError:
pass





Peter Otten
  Reply With Quote
Old 11-06-2009, 05:33 PM   #8
MRAB
 
Posts: n/a
Default Re: What is the best way to delete strings in a string list thatthat match certain pattern?
Peng Yu wrote:
> On Fri, Nov 6, 2009 at 10:42 AM, Robert P. J. Day <> wrote:
>> On Fri, 6 Nov 2009, Peng Yu wrote:
>>
>>> On Fri, Nov 6, 2009 at 3:05 AM, Diez B. Roggisch <> 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.
>

[snip]
You can replace the contents of a list like this:

l[:] = [i for i in l if i != 'a']



MRAB
  Reply With Quote
Old 11-06-2009, 11:57 PM   #9
Dave Angel
 
Posts: n/a
Default Re: What is the best way to delete strings in a string list thatthat match certain pattern?


Peng Yu wrote:
> On Fri, Nov 6, 2009 at 10:42 AM, Robert P. J. Day <> wrote:
>
>> On Fri, 6 Nov 2009, Peng Yu wrote:
>>
>>
>>> On Fri, Nov 6, 2009 at 3:05 AM, Diez B. Roggisch <> 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 ]


HTH
DaveA


Dave Angel
  Reply With Quote
Old 11-07-2009, 02:54 PM   #10
Steven D'Aprano
 
Posts: n/a
Default Re: What is the best way to delete strings in a string list thatthatmatch certain pattern?
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



--
Steven


Steven D'Aprano
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
passwordcard.org: pick and remember secure passwords in a low techand free way Pepijn Schmitz Computer Security 3 06-07-2009 07:42 AM
Java String Problems rbnbenjamin General Help Related Topics 0 02-03-2009 11:02 PM
ASP.NET: Asign Users in Roles(Array.IndexOf(Of String) method) msandlana Software 0 04-25-2008 06:37 AM
Hidden linebreaks in string? VB.NET Jiggy Software 0 04-23-2008 02:18 PM
Illegal operation carololine Computer Support 12 07-14-2006 01:27 PM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46