[Tutor] reducing a list evenly when deleting elements by index

Oscar Benjamin oscar.j.benjamin at gmail.com
Mon Sep 17 15:02:48 CEST 2012


On 2012-09-17, Steven D'Aprano <steve at pearwood.info> wrote:
> On 17/09/12 11:15, Pete O'Connell wrote:
>> Hi, I have a bezier line with 20 points on it and I am trying to reduce
>> this to a line with 4 points, keeping the first and last points and 2
>> evenly spaced points in between like so:
>
> In general in Python, it is faster and much easier to create a new list
> rather than to mess about deleting items from a list.
>
> So instead of calling del 16 times, which has to move list items around,
> it will be faster and simpler to create a new list:
>
> new = [old[0], old[6], old[13], old[19]]
>
> then just use the new list. If you really need to modify the old list in
> place (perhaps because other parts of the code need to see the same
> change) then you can do a slice-assignment:
>
> old[:] = new  # note the [:] slice
>

I certainly agree with Steven's comments here that there are much less
convulated ways to replace a list with a new list containing a subset of its
elements. I would also like to add, though, that I'm not sure if what you're
doing makes sense from a mathematical/geometrical viewpoint. It's not totally
clear to me from your original post what you're trying to do. I can think of
two interpretations:

1) You have a Bezier curver consisting of 20 control points and would like to
approximate it with a simpler cubic Bezier curve consisting of only 4 points.
I would call this "approximating a Bezier with a lower order Bezier".

2) You have a line that goes through 20 points and you would like to represent
it as a cubic Bezier with 4 control points. I would call this "fitting a
cubic Bezier to a sequence of points along a line".

In either case I think that choosing 4 of the points and using them as a cubic
Bezier is incorrect. For case 1) I don't think it's possible to approximate a
Bezier using a subset of its control points. I'm not sure how to prove it but
I think that typically a significantly higher order Bezier will have control
points that are closer to the curve than an equivalent lower order Bezier.

For case 2) it's not appropriate to think of a Bezier as interpolating its
control points because it doesn't go through it's control points (except for
the two at the ends).

In either case I would expect a good method to use information from all of the
original sequence of points rather than ignoring all but a small subset.

Oscar



More information about the Tutor mailing list