"Collapsing" a list into a list of changes

F

Fredrik Lundh

Peter said:
*You* are cheating when you take a predefined function implemented in C
(operator.mul) and then claim you are using pure Python.

operator is a standard module in Python. if you have a desperate need
to be pointless in public, consider using another newsgroup.

</F>
 
P

Peter Otten

Fredrik said:
operator is a standard module in Python. if you have a desperate need
to be pointless in public, consider using another newsgroup.

</F>

No.

Peter
 
R

Raymond Hettinger

[Alan McIntyre]
I have a list of items that has contiguous repetitions of values, but
the number and location of the repetitions is not important, so I just
need to strip them out. For example, if my original list is
[0,0,1,1,1,2,2,3,3,3,2,2,2,4,4,4,5], I want to end up with [0,1,2,3,2,4,5].
import itertools
[k for k, v in itertools.groupby(lst)]
[0, 1, 2, 3, 2, 4, 5]


Raymond Hettinger
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top