Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   iterating over collection, deleting entries (http://www.velocityreviews.com/forums/t330244-iterating-over-collection-deleting-entries.html)

Patrick von Harsdorf 04-25-2004 04:25 PM

iterating over collection, deleting entries
 
I want to iterate over a collection and delete all unwanted entries.

for item in collection:
del item

of course doesnīt do anything useful. Two things come to mind:
a) iterating backwards over the collection using indexing, something like:

for i in range(len(collection)-1, -1, -1):
item = collection[i]
if isBad(item) del collection[i]

b) duplicating the collection, iterating over one and deleting from the
other.

Both solutions seem both ugly and expensive to me, solution a)
probably isnīt even O(x) anymore.

Please tell me, whatīs the best way to do this?


--
Patrick von Harsdorf
patrick@harsdorf.de





Patrick von Harsdorf 04-25-2004 04:28 PM

Re: iterating over collection, deleting entries => PLEASE IGNORE
 
Sorry, I missed that SnuSnu just posed a very similar question.
Please ignore my post...



Andrea Griffini 04-25-2004 05:36 PM

Re: iterating over collection, deleting entries
 
On Sun, 25 Apr 2004 18:25:08 +0200, "Patrick von Harsdorf"
<patrick@harsdorf.de> wrote:

>Please tell me, whatīs the best way to do this?


I'm pretty new to python, but normally this is done
by a simple read-write approach. You iterate over
the elements using a "read" pointer, and every time
you find one you want to keep you just write it
and increment the write pointer.
In code...

wrp = 0

for x in container:
if is_good(x):
container[wrp] = x
wrp += 1

del container[wrp:]

This is O(n) and doesn't require any additional memory

I suppose however that it's not so common finding
cases in which this is much better than...

a = [x for x in a if is_good(x)]

The latter will allocate a list for the result
(instead of reusing the same), but can free the
original (and this may be *better* than just
resizing it - it may be better in C++, for example).

HTH
Andrea

Larry Bates 04-26-2004 07:48 PM

Re: iterating over collection, deleting entries
 
I would do:

collection=[i for i in collection if not isBad(i)]

Larry Bates
Syscon, Inc.
"Patrick von Harsdorf" <patrick@harsdorf.de> wrote in message
news:c6gop3$5p7$03$1@news.t-online.com...
> I want to iterate over a collection and delete all unwanted entries.
>
> for item in collection:
> del item
>
> of course doesnīt do anything useful. Two things come to mind:
> a) iterating backwards over the collection using indexing, something like:
>
> for i in range(len(collection)-1, -1, -1):
> item = collection[i]
> if isBad(item) del collection[i]
>
> b) duplicating the collection, iterating over one and deleting from the
> other.
>
> Both solutions seem both ugly and expensive to me, solution a)
> probably isnīt even O(x) anymore.
>
> Please tell me, whatīs the best way to do this?
>
>
> --
> Patrick von Harsdorf
> patrick@harsdorf.de
>
>
>
>





All times are GMT. The time now is 09:05 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.