Need help porting Perl function
socyl at 987jk.com.invalid
Sun Jun 8 18:50:19 CEST 2008
In <7248c81b-0ff9-45f5-acb5-63a8b2d41817 at f24g2000prh.googlegroups.com> John Machin <sjmachin at lexicon.net> writes:
>It's nothing to do with list comprehensions, which are syntactical
>sugar for traditional loops. You could rewrite your list comprehension
>in the traditional manner...
>and it would still fail for the same reason: mutating the list over
>which you are iterating.
I normally deal with this problem by iterating backwards over the
indices. Here's how I coded the function (in "Python-greenhorn
culled = 
for i in range(len(list) - 1, -1, -1):
...where not_wanted() is defined elsewhere. (For my purposes at the
moment, the greater generality provided by making not_wanted() a
parameter to cull() was not necessary.)
The specification of the indices at the beginning of the for-loop
looks pretty ignorant, but aside from that I'm happy with it.
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.
More information about the Python-list