Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
What is the best way to delete strings in a string list that that match certain pattern?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  19 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Peng Yu  
View profile  
 More options Nov 6, 3:19 pm
Newsgroups: comp.lang.python
From: Peng Yu <pengyu...@gmail.com>
Date: Thu, 5 Nov 2009 22:19:08 -0600
Local: Fri, Nov 6 2009 3:19 pm
Subject: 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.


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chris Rebert  
View profile  
 More options Nov 6, 3:25 pm
Newsgroups: comp.lang.python
From: Chris Rebert <c...@rebertia.com>
Date: Thu, 5 Nov 2009 20:25:24 -0800
Local: Fri, Nov 6 2009 3:25 pm
Subject: Re: What is the best way to delete strings in a string list that that match certain pattern?

On Thu, Nov 5, 2009 at 8:19 PM, Peng Yu <pengyu...@gmail.com> 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


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Peng Yu  
View profile  
 More options Nov 6, 4:23 pm
Newsgroups: comp.lang.python
From: Peng Yu <pengyu...@gmail.com>
Date: Thu, 5 Nov 2009 23:23:12 -0600
Local: Fri, Nov 6 2009 4:23 pm
Subject: Re: What is the best way to delete strings in a string list that that match certain pattern?

On Thu, Nov 5, 2009 at 10:25 PM, Chris Rebert <c...@rebertia.com> wrote:
> On Thu, Nov 5, 2009 at 8:19 PM, Peng Yu <pengyu...@gmail.com> 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?

    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chris Rebert  
View profile  
 More options Nov 6, 5:57 pm
Newsgroups: comp.lang.python
From: Chris Rebert <c...@rebertia.com>
Date: Thu, 5 Nov 2009 22:57:03 -0800
Local: Fri, Nov 6 2009 5:57 pm
Subject: Re: What is the best way to delete strings in a string list that that match certain pattern?

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


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Gabriel Genellina  
View profile  
 More options Nov 6, 5:59 pm
Newsgroups: comp.lang.python
From: "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
Date: Fri, 06 Nov 2009 03:59:39 -0300
Local: Fri, Nov 6 2009 5:59 pm
Subject: Re: What is the best way to delete strings in a string list that that match certain pattern?
En Fri, 06 Nov 2009 02:23:12 -0300, Peng Yu <pengyu...@gmail.com> escribió:

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


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Lie Ryan  
View profile  
 More options Nov 6, 6:30 pm
Newsgroups: comp.lang.python
From: Lie Ryan <lie.1...@gmail.com>
Date: Fri, 06 Nov 2009 18:30:18 +1100
Local: Fri, Nov 6 2009 6:30 pm
Subject: Re: What is the best way to delete strings in a string list that that 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])
         )
     )


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Diez B. Roggisch  
View profile  
 More options Nov 6, 8:05 pm
Newsgroups: comp.lang.python
From: "Diez B. Roggisch" <de...@nospam.web.de>
Date: Fri, 06 Nov 2009 10:05:30 +0100
Local: Fri, Nov 6 2009 8:05 pm
Subject: Re: What is the best way to delete strings in a string list that that 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


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Peng Yu  
View profile  
 More options Nov 7, 3:16 am
Newsgroups: comp.lang.python
From: Peng Yu <pengyu...@gmail.com>
Date: Fri, 6 Nov 2009 10:16:58 -0600
Local: Sat, Nov 7 2009 3:16 am
Subject: Re: What is the best way to delete strings in a string list that that match certain pattern?
On Fri, Nov 6, 2009 at 3:05 AM, Diez B. Roggisch <de...@nospam.web.de> 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


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Robert P. J. Day  
View profile  
 More options Nov 7, 3:42 am
Newsgroups: comp.lang.python
From: "Robert P. J. Day" <rpj...@crashcourse.ca>
Date: Fri, 6 Nov 2009 11:42:45 -0500 (EST)
Local: Sat, Nov 7 2009 3:42 am
Subject: Re: What is the best way to delete strings in a string list that that match certain pattern?

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


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Peng Yu  
View profile  
 More options Nov 7, 3:58 am
Newsgroups: comp.lang.python
From: Peng Yu <pengyu...@gmail.com>
Date: Fri, 6 Nov 2009 10:58:13 -0600
Local: Sat, Nov 7 2009 3:58 am
Subject: Re: What is the best way to delete strings in a string list that that match certain pattern?
On Fri, Nov 6, 2009 at 10:42 AM, Robert P. J. Day <rpj...@crashcourse.ca> 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.

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.


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Peter Otten  
View profile  
 More options Nov 7, 4:05 am
Newsgroups: comp.lang.python
Followup-To: comp.lang.python
From: Peter Otten <__pete...@web.de>
Date: Fri, 06 Nov 2009 18:05:11 +0100
Local: Sat, Nov 7 2009 4:05 am
Subject: 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


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
MRAB  
View profile  
 More options Nov 7, 4:33 am
Newsgroups: comp.lang.python
From: MRAB <pyt...@mrabarnett.plus.com>
Date: Fri, 06 Nov 2009 17:33:54 +0000
Local: Sat, Nov 7 2009 4:33 am
Subject: Re: What is the best way to delete strings in a string list that that match certain pattern?

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

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


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Terry Reedy  
View profile  
 More options Nov 7, 8:58 am
Newsgroups: comp.lang.python
From: Terry Reedy <tjre...@udel.edu>
Date: Fri, 06 Nov 2009 16:58:31 -0500
Local: Sat, Nov 7 2009 8:58 am
Subject: Re: What is the best way to delete strings in a string list that that match certain pattern?

Peng Yu wrote:
> Now, I want to in-place delete elements in A that matches the regex.

You can to this with a for-loop *IF YOU ITERATE BACKWARDS*.
It is O(n**2) in anycase.

    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dave Angel  
View profile  
 More options Nov 7, 10:57 am
Newsgroups: comp.lang.python
From: Dave Angel <da...@ieee.org>
Date: Fri, 06 Nov 2009 18:57:28 -0500
Local: Sat, Nov 7 2009 10:57 am
Subject: Re: What is the best way to delete strings in a string list that that match certain pattern?

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


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Steven D'Aprano  
View profile  
 More options Nov 8, 1:54 am
Newsgroups: comp.lang.python
From: Steven D'Aprano <st...@REMOVE-THIS-cybersource.com.au>
Date: 07 Nov 2009 14:54:18 GMT
Local: Sun, Nov 8 2009 1:54 am
Subject: Re: What is the best way to delete strings in a string list that that match 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/articles/fog0000000319.html

--
Steven


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Peng Yu  
View profile  
 More options Nov 8, 3:12 am
Newsgroups: comp.lang.python
From: Peng Yu <pengyu...@gmail.com>
Date: Sat, 7 Nov 2009 10:12:25 -0600
Local: Sun, Nov 8 2009 3:12 am
Subject: Re: What is the best way to delete strings in a string list that that match certain pattern?
On Sat, Nov 7, 2009 at 8:54 AM, Steven D'Aprano

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    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Peng Yu  
View profile  
 More options Nov 8, 3:13 am
Newsgroups: comp.lang.python
From: Peng Yu <pengyu...@gmail.com>
Date: Sat, 7 Nov 2009 10:13:52 -0600
Local: Sun, Nov 8 2009 3:13 am
Subject: Re: What is the best way to delete strings in a string list that that match certain pattern?

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

    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Robert P. J. Day  
View profile  
 More options Nov 8, 5:20 am
Newsgroups: comp.lang.python
From: "Robert P. J. Day" <rpj...@crashcourse.ca>
Date: Sat, 7 Nov 2009 13:20:47 -0500 (EST)
Local: Sun, Nov 8 2009 5:20 am
Subject: Re: What is the best way to delete strings in a string list that that match certain pattern?

On Sat, 7 Nov 2009, Peng Yu wrote:
> On Fri, Nov 6, 2009 at 5:57 PM, Dave Angel <da...@ieee.org> 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    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Peter Otten  
View profile  
 More options Nov 8, 5:47 am
Newsgroups: comp.lang.python
Followup-To: comp.lang.python
From: Peter Otten <__pete...@web.de>
Date: Sat, 07 Nov 2009 19:47:41 +0100
Local: Sun, Nov 8 2009 5:47 am
Subject: Re: What is the best way to delete strings in a string list that that match certain pattern?
Robert P. J. Day wrote:

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    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google