Re: "Collapsing" a list into a list of changes

2005-02-09 Thread 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

Re: "Collapsing" a list into a list of changes

2005-02-07 Thread Peter Otten
Fredrik Lundh wrote: > Peter Otten wrote: > >> *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 usin

Re: "Collapsing" a list into a list of changes

2005-02-07 Thread Fredrik Lundh
Peter Otten wrote: > *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. -- htt

Re: "Collapsing" a list into a list of changes

2005-02-07 Thread Peter Otten
John Lenton wrote: > On Mon, Feb 07, 2005 at 09:39:11PM +0100, Peter Otten wrote: >> John Lenton wrote: >> > For example, the fastest way >> > to get the factorial of a (small enough) number in pure python is >> > >> > factorial = lambda n: reduce(operato

Re: "Collapsing" a list into a list of changes

2005-02-07 Thread Francis Girard
Le lundi 7 Février 2005 22:53, Steven Bethard a écrit : > Francis Girard wrote: > > I see. I personnaly use them frequently to bind an argument of a function > > with some fixed value. Modifying one of the example in > > http://mail.python.org/pipermail/python-list/2004-December/257990.html > > I f

Re: "Collapsing" a list into a list of changes

2005-02-07 Thread Steven Bethard
Francis Girard wrote: I see. I personnaly use them frequently to bind an argument of a function with some fixed value. Modifying one of the example in http://mail.python.org/pipermail/python-list/2004-December/257990.html I frequently have something like : SimpleXMLRPCServer.py: server.register

Re: "Collapsing" a list into a list of changes

2005-02-07 Thread Francis Girard
Le lundi 7 Février 2005 21:21, Steven Bethard a écrit : > Francis Girard wrote: > > Le lundi 7 Février 2005 20:30, Steven Bethard a écrit : > >>especially since I avoid lambda usage, and would have to write these as: > > > > Why avoid "lambda" usage ? You find them too difficult to read (I mean in

Re: "Collapsing" a list into a list of changes

2005-02-07 Thread John Lenton
On Mon, Feb 07, 2005 at 09:39:11PM +0100, Peter Otten wrote: > John Lenton wrote: > > For example, the fastest way > > to get the factorial of a (small enough) number in pure python is > > > > factorial = lambda n: reduce(operator.mul, range(1, n+1)) > >

Re: "Collapsing" a list into a list of changes

2005-02-07 Thread Peter Otten
John Lenton wrote: > On Mon, Feb 07, 2005 at 07:07:10PM +0100, Francis Girard wrote: >> Zut ! >> >> I'm very sorry that there is no good use case for the "reduce" function >> in Python, like Peter Otten pretends. That's an otherwise very useful >> tool for many use cases. At least on paper. >> >

Re: "Collapsing" a list into a list of changes

2005-02-07 Thread Steven Bethard
Francis Girard wrote: Le lundi 7 Février 2005 20:30, Steven Bethard a écrit : especially since I avoid lambda usage, and would have to write these as: Why avoid "lambda" usage ? You find them too difficult to read (I mean in general) ? Yup, basically a readability thing. I also tend to find that

Re: "Collapsing" a list into a list of changes

2005-02-07 Thread Francis Girard
Le lundi 7 Février 2005 20:30, Steven Bethard a écrit : > Francis Girard wrote: > > Is there someone on this list using this tool and happy with it ? Or is > > my mind too much targeted on FP paradigm and most of you really think > > that all the functions that apply another function to each and ev

Re: "Collapsing" a list into a list of changes

2005-02-07 Thread Francis Girard
Le lundi 7 FÃvrier 2005 19:51, John Lenton a ÃcritÂ: > On Mon, Feb 07, 2005 at 07:07:10PM +0100, Francis Girard wrote: > > Zut ! > > > > I'm very sorry that there is no good use case for the "reduce" function > > in Python, like Peter Otten pretends. That's an otherwise very useful > > tool for man

Re: "Collapsing" a list into a list of changes

2005-02-07 Thread Steven Bethard
John Lenton wrote: For example, the fastest way to get the factorial of a (small enough) number in pure python is factorial = lambda n: reduce(operator.mul, range(1, n+1)) Gah! I'll never understand why people use lambda, which is intended to create _anonymous_ functions, to create named functi

Re: "Collapsing" a list into a list of changes

2005-02-07 Thread Steven Bethard
Francis Girard wrote: Is there someone on this list using this tool and happy with it ? Or is my mind too much targeted on FP paradigm and most of you really think that all the functions that apply another function to each and every elements of a list are bad (like "reduce", "map", "filter") ? I

Re: "Collapsing" a list into a list of changes

2005-02-07 Thread Francis Girard
No. Just wanted to naively use one of the base tool Python had to offer. I tought it was very usable and very readable. I might be wrong. Is there someone on this list using this tool and happy with it ? Or is my mind too much targeted on FP paradigm and most of you really think that all the fu

Re: "Collapsing" a list into a list of changes

2005-02-07 Thread John Lenton
On Mon, Feb 07, 2005 at 07:07:10PM +0100, Francis Girard wrote: > Zut ! > > I'm very sorry that there is no good use case for the "reduce" function in > Python, like Peter Otten pretends. That's an otherwise very useful tool for > many use cases. At least on paper. > > Python documentation sho

Re: "Collapsing" a list into a list of changes

2005-02-07 Thread Steven Bethard
Francis Girard wrote: I'm very sorry that there is no good use case for the "reduce" function in Python, like Peter Otten pretends. That's an otherwise very useful tool for many use cases. At least on paper. Clarity aside[1], can you give an example of where reduce is as efficient as the eqival

Re: "Collapsing" a list into a list of changes

2005-02-07 Thread Peter Otten
Francis Girard wrote: > Python documentation should say "There is no good use case for the reduce > function in Python and we don't know why we bother you offering it." Submit a patch :-) Peter -- http://mail.python.org/mailman/listinfo/python-list

Re: "Collapsing" a list into a list of changes

2005-02-07 Thread Francis Girard
Zut ! I'm very sorry that there is no good use case for the "reduce" function in Python, like Peter Otten pretends. That's an otherwise very useful tool for many use cases. At least on paper. Python documentation should say "There is no good use case for the reduce function in Python and we d

Re: "Collapsing" a list into a list of changes

2005-02-07 Thread Adam Przybyla
Alan McIntyre <[EMAIL PROTECTED]> 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

Re: "Collapsing" a list into a list of changes

2005-02-06 Thread Peter Otten
Francis Girard wrote: > This is a prefect use case for the good old "reduce" function: > > --BEGIN SNAP > > a_lst = [None,0,0,1,1,1,2,2,3,3,3,2,2,2,4,4,4,5] > > def straightforward_collapse(lst): > return reduce(lambda v,e: v[-1]!=e and v+[e] or v, lst[1:], [lst[0]]) reduce() magically increa

Re: "Collapsing" a list into a list of changes

2005-02-06 Thread Francis Girard
This is a prefect use case for the good old "reduce" function: --BEGIN SNAP a_lst = [None,0,0,1,1,1,2,2,3,3,3,2,2,2,4,4,4,5] def straightforward_collapse(lst): return reduce(lambda v,e: v[-1]!=e and v+[e] or v, lst[1:], [lst[0]]) def straightforward_collapse_secu(lst): return lst and reduce

Re: "Collapsing" a list into a list of changes

2005-02-06 Thread Alan McIntyre
Thanks to everybody that responded; I appreciate all the input, even if I didn't respond to all of it individually. :) -- http://mail.python.org/mailman/listinfo/python-list

Re: "Collapsing" a list into a list of changes

2005-02-05 Thread Steven Bethard
Alex Martelli wrote: Steven Bethard <[EMAIL PROTECTED]> wrote: Here's a solution that works for iterables other than lists: py> def collapse(iterable): ... enumeration = enumerate(iterable) ... _, lastitem = enumeration.next() ... yield lastitem ... for i, item in enumeration: ...

Re: "Collapsing" a list into a list of changes

2005-02-05 Thread Tony
Alan McIntyre wrote: > Tony, > > Actually I only want to remove a certain kind of duplication; How about this one liner? def condense(m): print [m[0]]+[m[k] for k in range(1,len(m)) if m[k]!=m[k-1]] b=[1,1,1,2,2,2,1,1,1] condense(b) Tony Clarke -- http://mail.python.org/mai

Re: "Collapsing" a list into a list of changes

2005-02-05 Thread Jack Diederich
On Sat, Feb 05, 2005 at 02:31:08PM +1000, Nick Coghlan wrote: > Jack Diederich wrote: > >Since this is 2.4 you could also return a generator expression. > > > > > def iter_collapse(myList): > > > >... return (x[0] for (x) in > >it.groupby([0,0,1,1,1,2,2,3,3,3,2,2,2,4,4,4,5])) > >... > > Bu

Re: "Collapsing" a list into a list of changes

2005-02-05 Thread Alan McIntyre
Alex, Wow, that method turns out to be the fastest so far in a simple benchmark on Python2.3 (on my machine, of course, YMMV); it takes 14% less time than the one that I deemed most straightforward. :) Thanks, Alan Alex Martelli wrote: H, what role does the enumeration play here? I don't se

Re: "Collapsing" a list into a list of changes

2005-02-05 Thread Alan McIntyre
Tony, Actually I only want to remove a certain kind of duplication; if an item occurs twice - say like this: [1,1,1,2,2,2,1,1,1], then I need to keep the order and occurrence of the individual values: [1,2,1]. Using a dict as you proposed loses the order of occurrence, as well as multiple occu

Re: "Collapsing" a list into a list of changes

2005-02-05 Thread Alan McIntyre
Steve, Yeah, in this particular application the ordering and reoccurrence of a value in a non-contiguous way does matter; if those two things weren't required I think the method you suggested would be a good way to remove the duplicates. Thanks! Coates, Steve (ACHE) wrote: It's not _exactly_ wh

Re: "Collapsing" a list into a list of changes

2005-02-05 Thread Tony
Alan McIntyre 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

Re: "Collapsing" a list into a list of changes

2005-02-05 Thread Alex Martelli
Steven Bethard <[EMAIL PROTECTED]> wrote: > Here's a solution that works for iterables other than lists: > > py> def collapse(iterable): > ... enumeration = enumerate(iterable) > ... _, lastitem = enumeration.next() > ... yield lastitem > ... for i, item in enumeration: > ...

RE: "Collapsing" a list into a list of changes

2005-02-05 Thread Coates, Steve (ACHE)
me as l. That may or may not be a problem for you. Regards Steve > -Original Message- > From: Alan McIntyre [mailto:[EMAIL PROTECTED] > Sent: 04 February 2005 17:44 > To: python-list@python.org > Subject: "Collapsing" a list into a list of changes > > Hi all,

Re: "Collapsing" a list into a list of changes

2005-02-04 Thread Nick Coghlan
Jack Diederich wrote: Since this is 2.4 you could also return a generator expression. def iter_collapse(myList): ... return (x[0] for (x) in it.groupby([0,0,1,1,1,2,2,3,3,3,2,2,2,4,4,4,5])) ... But why write a function that returns a generator expression, when you could just turn the function

Re: "Collapsing" a list into a list of changes

2005-02-04 Thread Nick Coghlan
John J. Lee wrote: Steven Bethard <[EMAIL PROTECTED]> writes: Mike C. Fletcher wrote: [...] >>> def changes( dataset ): ... last = None ... for value in dataset: ... if value != last: ... yield value ... last = value ...>>> print list(changes(data )) whi

Re: "Collapsing" a list into a list of changes

2005-02-04 Thread John J. Lee
Steven Bethard <[EMAIL PROTECTED]> writes: > Mike C. Fletcher wrote: [...] > > >>> def changes( dataset ): > > ... last = None > > ... for value in dataset: > > ... if value != last: > > ... yield value > > ... last = value > > ...>>> print list(changes

Re: "Collapsing" a list into a list of changes

2005-02-04 Thread Steven Bethard
Mike C. Fletcher wrote: Alan McIntyre wrote: ... 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 t

Re: "Collapsing" a list into a list of changes

2005-02-04 Thread Mike C. Fletcher
Alan McIntyre wrote: ... 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,

Re: "Collapsing" a list into a list of changes

2005-02-04 Thread Alan McIntyre
Jack, I'm not using 2.4 yet; still back in 2.3x. :) Thanks for the examples, though - they are clear enough that I will probably use them when I upgrade. Thanks, Alan Jack Diederich wrote: If you are using python2.4, import itertools as it [x[0] for (x) in it.groupby([0,0,1,1,1,2,2,3,3,3,2,2,2,

Re: "Collapsing" a list into a list of changes

2005-02-04 Thread Alan McIntyre
I think you're right; sometimes I'm susceptible to the "I can do that in one line of code" temptation. :) Since this current bit of code will probably end up in something that's going to be maintained, I will probably stick with the straightforward method just to be nice to the maintainer (espe

Re: "Collapsing" a list into a list of changes

2005-02-04 Thread Steven Bethard
Alan McIntyre 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

Re: "Collapsing" a list into a list of changes

2005-02-04 Thread Alan McIntyre
Your first example is along the lines of what I was thinking when I said "elegant." :) I was looking for something that I could drop into one or two lines of code; I may not do that if I'm writing code that will have to be maintained, but it's still nice to know how to do it. Thanks :) Alan

Re: "Collapsing" a list into a list of changes

2005-02-04 Thread Jack Diederich
On Fri, Feb 04, 2005 at 12:43:37PM -0500, Alan McIntyre 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

Re: "Collapsing" a list into a list of changes

2005-02-04 Thread Jeremy Bowers
On Fri, 04 Feb 2005 12:43:37 -0500, Alan McIntyre wrote: > 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 sho

Re: "Collapsing" a list into a list of changes

2005-02-04 Thread Steven Bethard
Alan McIntyre 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

"Collapsing" a list into a list of changes

2005-02-04 Thread Alan McIntyre
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 t