"Collapsing" a list into a list of changes
Adam Przybyla
adam at gliwice.pl
Mon Feb 7 07:41:12 EST 2005
More information about the Python-list mailing list
Mon Feb 7 07:41:12 EST 2005
- Previous message (by thread): "Collapsing" a list into a list of changes
- Next message (by thread): "Collapsing" a list into a list of changes
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Alan McIntyre <alan.mcintyre at esrgtech.com> wrote: > Hi all, > > 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]. > > Here is the way I'm doing this now: > > def straightforward_collapse(myList): > collapsed = [myList[0]] > for n in myList[1:]: > if n != collapsed[-1]: > collapsed.append(n) > > return collapsed > > Is there an elegant way to do this, or should I just stick with the code > above? >>> p=[1,1,1,1,1,4,4,4,8,8,9] >>> filter(lambda y: y>0, map(lambda x,y: x==y and -1 or x,[0]+p,p+[0])) [1, 4, 8, 9] >>> Z powazaniem Adam Przybyla
- Previous message (by thread): "Collapsing" a list into a list of changes
- Next message (by thread): "Collapsing" a list into a list of changes
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list