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

Mailing List Archive: Python: Python

Re: 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


deets at nospam

Nov 6, 2009, 1:05 AM

Post #1 of 3 (67 views)
Permalink
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
--
http://mail.python.org/mailman/listinfo/python-list


pengyu.ut at gmail

Nov 6, 2009, 8:16 AM

Post #2 of 3 (59 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 3:05 AM, Diez B. Roggisch <deets[at]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
--
http://mail.python.org/mailman/listinfo/python-list


tjreedy at udel

Nov 6, 2009, 1:58 PM

Post #3 of 3 (50 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:

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

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

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


Interested in having your list archived? Contact lists@gossamer-threads.com
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.