Algorithm help per favore
Sean Ross
frobozz_electric at hotmail.com
Wed Jun 18 12:27:40 EDT 2003
"Larry" <wrbt at email.com> 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]
>
# Chris Perkins:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/204297
def thislist():
"""Return a reference to the list object being constructed by the
list comprehension from which this function is called. Raises an
exception if called from anywhere else.
"""
import sys
d = sys._getframe(1).f_locals
nestlevel = 1
while '_[%d]' % nestlevel in d:
nestlevel += 1
return d['_[%d]' % (nestlevel - 1)].__self__
def last(sequence):
"return the last item in list, or empty list"
if sequence:
return sequence[-1]
else:
return sequence
sequence = [6,3,3,3,5,7,6,3,4,4,3]
filtered = [item for item in sequence if item != last(thislist())]
print filtered
# OUTPUT
#>>> [6, 3, 5, 7, 6, 3, 4, 3]
The idea here is to do something like this:
lastitem = sequence[0]
filtered = [lastitem]
for item in sequence[1:]:
if item != lastitem:
filtered.append(item)
lastitem = item
But here we make use of Chris Perkins recipe to gain access to the list
being contructed by the list comprehension that is filtering our original
sequence.
More information about the Python-list
mailing list