Algorithm help per favore

Paul Simmonds psimmo60 at hotmail.com
Thu Jun 19 03:32:53 EDT 2003


wrbt at email.com (Larry) wrote in message news:<2ec1bc1c.0306180746.159679d6 at posting.google.com>...
> I need to take a list (probably 20k to 40k elements) of numbers and
> remove consecutive duplicates. Non consecutive duplicates are ok.
> 
> Example: [6,3,3,3,5,7,6,3,4,4,3] => [6,3,5,7,6,3,4,3]
> 
> The 3 and 6 can appear more than once in the result set because
> they're separated by another value. Obviously this is trivial to
> accomplish by walking thru the list and building a new one (or yanking
> elements out of the existing one) but I'm curious if anyone knows of a
> more clever way, with speed being a goal more than memory usage.

Another list comp method, taking a slightly different angle:

[lst[i] for i in range(len(lst)) if(i!=len(lst)-1 and
lst[i]!=lst[i+1])]

Iterating over a list of integers rather than a modified list of
objects seems to be faster. This would depend how the list was
modified, but the len method is in C, and I'm guessing the range
method is too.

HTH,
Paul




More information about the Python-list mailing list