[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
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
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
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
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
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
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
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))
>
>
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.
>>
>
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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:
...
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
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
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
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
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
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
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:
> ...
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,
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
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
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
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
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,
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,
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
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
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
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
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
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
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
45 matches
Mail list logo