Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

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

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


pengyu.ut at gmail

Nov 5, 2009, 8:19 PM

Post #1 of 13 (505 views)
Permalink
What is the best way to delete strings in a string list that that match 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.
--
http://mail.python.org/mailman/listinfo/python-list


clp2 at rebertia

Nov 5, 2009, 8:25 PM

Post #2 of 13 (488 views)
Permalink
Re: What is the best way to delete strings in a string list that that match certain pattern? [In reply to]

On Thu, Nov 5, 2009 at 8:19 PM, Peng Yu <pengyu.ut [at] gmail> 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.
>
> I'm wondering what is the best way to compute B in python.

Since this sounds rather homework-y, I'll only give you a pointer:
http://docs.python.org/tutorial/datastructures.html#list-comprehensions

Cheers,
Chris
--
http://blog.rebertia.com
--
http://mail.python.org/mailman/listinfo/python-list


pengyu.ut at gmail

Nov 5, 2009, 9:23 PM

Post #3 of 13 (488 views)
Permalink
Re: What is the best way to delete strings in a string list that that match certain pattern? [In reply to]

On Thu, Nov 5, 2009 at 10:25 PM, Chris Rebert <clp2 [at] rebertia> wrote:
> On Thu, Nov 5, 2009 at 8:19 PM, Peng Yu <pengyu.ut [at] gmail> 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.
>>
>> I'm wondering what is the best way to compute B in python.
>
> Since this sounds rather homework-y, I'll only give you a pointer:
> http://docs.python.org/tutorial/datastructures.html#list-comprehensions

Now, I want to in-place delete elements in A that matches the regex. I
know that I need to use del. But I'm not sure how to use the
functional style programming for this problem. Would you please let me
know?
--
http://mail.python.org/mailman/listinfo/python-list


clp2 at rebertia

Nov 5, 2009, 10:57 PM

Post #4 of 13 (477 views)
Permalink
Re: What is the best way to delete strings in a string list that that match certain pattern? [In reply to]

On Thu, Nov 5, 2009 at 9:23 PM, Peng Yu <pengyu.ut [at] gmail> wrote:
> On Thu, Nov 5, 2009 at 10:25 PM, Chris Rebert <clp2 [at] rebertia> wrote:
>> On Thu, Nov 5, 2009 at 8:19 PM, Peng Yu <pengyu.ut [at] gmail> 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.
>>>
>>> I'm wondering what is the best way to compute B in python.
>>
>> Since this sounds rather homework-y, I'll only give you a pointer:
>> http://docs.python.org/tutorial/datastructures.html#list-comprehensions
>
> Now, I want to in-place delete elements in A that matches the regex. I
> know that I need to use del. But I'm not sure how to use the
> functional style programming for this problem. Would you please let me
> know?

Deletion is an imperative operation which has no direct equivalent in
functional languages, so your question is nonsensical.
To do it functionally, instead of deleting, you simply build a new
list that omits the undesired elements.
See also the built-in function filter().

Cheers,
Chris
--
http://blog.rebertia.com
--
http://mail.python.org/mailman/listinfo/python-list


gagsl-py2 at yahoo

Nov 5, 2009, 10:59 PM

Post #5 of 13 (477 views)
Permalink
Re: What is the best way to delete strings in a string list that that match certain pattern? [In reply to]

En Fri, 06 Nov 2009 02:23:12 -0300, Peng Yu <pengyu.ut [at] gmail> escribió:
> On Thu, Nov 5, 2009 at 10:25 PM, Chris Rebert <clp2 [at] rebertia> wrote:
>> On Thu, Nov 5, 2009 at 8:19 PM, Peng Yu <pengyu.ut [at] gmail> 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.
>>>
>>> I'm wondering what is the best way to compute B in python.
>>
>> Since this sounds rather homework-y, I'll only give you a pointer:
>> http://docs.python.org/tutorial/datastructures.html#list-comprehensions
>
> Now, I want to in-place delete elements in A that matches the regex. I
> know that I need to use del. But I'm not sure how to use the
> functional style programming for this problem. Would you please let me
> know?

Functional and del don't mix. What about:

B = [item for item in A if regex.match(item) is None]
B = filter(lambda item: regex.match(item) is None, A)

--
Gabriel Genellina

--
http://mail.python.org/mailman/listinfo/python-list


lie.1296 at gmail

Nov 5, 2009, 11:30 PM

Post #6 of 13 (482 views)
Permalink
Re: What is the best way to delete strings in a string list that that match certain pattern? [In reply to]

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])
)
)
--
http://mail.python.org/mailman/listinfo/python-list


rpjday at crashcourse

Nov 6, 2009, 8:42 AM

Post #7 of 13 (474 views)
Permalink
Re: What is the best way to delete strings in a string list that that match certain pattern? [In reply to]

On Fri, 6 Nov 2009, Peng Yu wrote:

> On Fri, Nov 6, 2009 at 3:05 AM, Diez B. Roggisch <deets [at] nospam> 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
========================================================================
--
http://mail.python.org/mailman/listinfo/python-list


pengyu.ut at gmail

Nov 6, 2009, 8:58 AM

Post #8 of 13 (476 views)
Permalink
Re: What is the best way to delete strings in a string list that that match certain pattern? [In reply to]

On Fri, Nov 6, 2009 at 10:42 AM, Robert P. J. Day <rpjday [at] crashcourse> wrote:
> On Fri, 6 Nov 2009, Peng Yu wrote:
>
>> On Fri, Nov 6, 2009 at 3:05 AM, Diez B. Roggisch <deets [at] nospam> 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.
--
http://mail.python.org/mailman/listinfo/python-list


__peter__ at web

Nov 6, 2009, 9:05 AM

Post #9 of 13 (474 views)
Permalink
Re: What is the best way to delete strings in a string list that that match certain pattern? [In reply to]

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



--
http://mail.python.org/mailman/listinfo/python-list


steve at REMOVE-THIS-cybersource

Nov 7, 2009, 6:54 AM

Post #10 of 13 (457 views)
Permalink
Re: What is the best way to delete strings in a string list that that match certain pattern? [In reply to]

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/articles/fog0000000319.html



--
Steven
--
http://mail.python.org/mailman/listinfo/python-list


pengyu.ut at gmail

Nov 7, 2009, 8:12 AM

Post #11 of 13 (460 views)
Permalink
Re: What is the best way to delete strings in a string list that that match certain pattern? [In reply to]

On Sat, Nov 7, 2009 at 8:54 AM, Steven D'Aprano
<steve [at] remove-this-cybersource> 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/articles/fog0000000319.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?
--
http://mail.python.org/mailman/listinfo/python-list


rpjday at crashcourse

Nov 7, 2009, 10:20 AM

Post #12 of 13 (456 views)
Permalink
Re: What is the best way to delete strings in a string list that that match certain pattern? [In reply to]

On Sat, 7 Nov 2009, Peng Yu wrote:

> On Fri, Nov 6, 2009 at 5:57 PM, Dave Angel <davea [at] ieee> 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
========================================================================


__peter__ at web

Nov 7, 2009, 10:47 AM

Post #13 of 13 (457 views)
Permalink
Re: What is the best way to delete strings in a string list that that match certain pattern? [In reply to]

Robert P. J. Day wrote:

> On Sat, 7 Nov 2009, Peng Yu wrote:
>
>> On Fri, Nov 6, 2009 at 5:57 PM, Dave Angel <davea [at] ieee> 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

--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.