Re: Finding the insertion point in a list

2007-03-18 Thread Alex Martelli
7stud <[EMAIL PROTECTED]> wrote: > On Mar 18, 2:23 am, Paul Rubin wrote: > > Steve Holden <[EMAIL PROTECTED]> writes: > > > >max(i for i,t in enumerate(x) if t <= y) > > > > Those are actually pretty direct. > > > > > How about a solution (like the bisect one suggest

Re: Finding the insertion point in a list

2007-03-18 Thread 7stud
On Mar 18, 2:23 am, Paul Rubin wrote: > Steve Holden <[EMAIL PROTECTED]> writes: > > >max(i for i,t in enumerate(x) if t <= y) > > > Those are actually pretty direct. > > > How about a solution (like the bisect one suggested almost as soon as > > this thread started)

Re: Finding the insertion point in a list

2007-03-18 Thread Paul Rubin
Steve Holden <[EMAIL PROTECTED]> writes: > >max(i for i,t in enumerate(x) if t <= y) > > Those are actually pretty direct. > > How about a solution (like the bisect one suggested almost as soon as > this thread started) that doesn't iterate over the whole list. Here's a Haskell-inspired one:

Re: Finding the insertion point in a list

2007-03-17 Thread 7stud
On Mar 17, 4:12 am, "Martin Blume" <[EMAIL PROTECTED]> wrote: > "7stud" schrieb > > > > > How about: > > > --- > > x = [0, 100, 200, 1000] > > y = -1 > > inserted = False > > > for i in range(len(x)): > > if(y <= x[i]): > > x.insert(i, y) > > inserted

Re: Finding the insertion point in a list

2007-03-17 Thread John Machin
On Mar 17, 9:46 pm, Steve Holden <[EMAIL PROTECTED]> wrote: > Paul Rubin wrote: > > Steven D'Aprano <[EMAIL PROTECTED]> writes: > >> or even > > >> len(filter(lambda t, y=y: y>t, x)) > > > How about > > >min(i for i,t in enumerate(x) if t >= y) > > > or > > >max(i for i,t in enumerate(x) if

Re: Finding the insertion point in a list

2007-03-17 Thread Steve Holden
Paul Rubin wrote: > Steven D'Aprano <[EMAIL PROTECTED]> writes: >> or even >> >> len(filter(lambda t, y=y: y>t, x)) > > > How about > >min(i for i,t in enumerate(x) if t >= y) > > or > >max(i for i,t in enumerate(x) if t <= y) > > Those are actually pretty direct. How about a soluti

Re: Finding the insertion point in a list

2007-03-17 Thread John Machin
On Mar 17, 5:42 pm, Paul Rubin wrote: > Steven D'Aprano <[EMAIL PROTECTED]> writes: > > or even > > > len(filter(lambda t, y=y: y>t, x)) > > How about > >min(i for i,t in enumerate(x) if t >= y) > > or > >max(i for i,t in enumerate(x) if t <= y) > > Those are actu

Re: Finding the insertion point in a list

2007-03-17 Thread Martin Blume
"7stud" schrieb > How about: > > --- > x = [0, 100, 200, 1000] > y = -1 > inserted = False > > for i in range(len(x)): > if(y <= x[i]): > x.insert(i, y) > inserted = True > break > if(not inserted): x.append(y) > > print x > --

Re: Finding the insertion point in a list

2007-03-16 Thread Paul Rubin
Steven D'Aprano <[EMAIL PROTECTED]> writes: > or even > > len(filter(lambda t, y=y: y>t, x)) How about min(i for i,t in enumerate(x) if t >= y) or max(i for i,t in enumerate(x) if t <= y) Those are actually pretty direct. -- http://mail.python.org/mailman/listinfo/python-list

Re: Finding the insertion point in a list

2007-03-16 Thread Paul Rubin
Steven D'Aprano <[EMAIL PROTECTED]> writes: > (1) It's wrong. That always returns the length of the list. Perhaps you > meant something like this? > len(["anything will do" for t in x if y > t]) Yeah, that's what I meant. -- http://mail.python.org/mailman/listinfo/python-list

Re: Finding the insertion point in a list

2007-03-16 Thread Steven D'Aprano
On Fri, 16 Mar 2007 18:17:08 -0800, Paul Rubin wrote: > [EMAIL PROTECTED] writes: >> Or with a generator comprehension >> n = sum ( y>x[i] for i in range(len(x)) ) - 1 >> >> But there has to be a cleaner way, as the first approach is unwieldy >> and does not adapt to changing list lengths, and

Re: Finding the insertion point in a list

2007-03-16 Thread Paul Rubin
[EMAIL PROTECTED] writes: > Or with a generator comprehension > n = sum ( y>x[i] for i in range(len(x)) ) - 1 > > But there has to be a cleaner way, as the first approach is unwieldy > and does not adapt to changing list lengths, and the second is not > obvious to a casual reader of the code. H

Re: Finding the insertion point in a list

2007-03-16 Thread tobiaskk
Or like this: x = [0, 100, 200, 1000] y = 435 for n, i in enumerate(x): if y < i: n = n - 1 break x.insert(n + 1, y) If you decide to stick with n = sum ( y>x[i] for i in range(len(x)) ) - 1 Replace it with:

Re: Finding the insertion point in a list

2007-03-16 Thread Matimus
On Mar 16, 11:20 am, "Matimus" <[EMAIL PROTECTED]> wrote: > You might look at the bisect module (part of the standard > distribution). Here is an example: >>> from bisect import insort >>> x = [0,100,200,1000] >>> insort(x,10) >>> x [0, 10, 100, 200, 1000] -- http://mail.python.org/mailman/list

Re: Finding the insertion point in a list

2007-03-16 Thread 7stud
How about: --- x = [0, 100, 200, 1000] y = -1 inserted = False for i in range(len(x)): if(y <= x[i]): x.insert(i, y) inserted = True break if(not inserted): x.append(y) print x -- http://mail.python.org/mailman/listin

Re: Finding the insertion point in a list

2007-03-16 Thread kyosohma
On Mar 16, 2:32 pm, "Paul McGuire" <[EMAIL PROTECTED]> wrote: > On Mar 16, 12:59 pm, [EMAIL PROTECTED] wrote: > > > > > I have an ordered list e.g. x = [0, 100, 200, 1000], and given any > > positive integer y, I want to determine its appropriate position in > > the list (i.e the point at which I w

Re: Finding the insertion point in a list

2007-03-16 Thread Paul McGuire
On Mar 16, 12:59 pm, [EMAIL PROTECTED] wrote: > I have an ordered list e.g. x = [0, 100, 200, 1000], and given any > positive integer y, I want to determine its appropriate position in > the list (i.e the point at which I would have to insert it in order to > keep the list sorted. I can clearly do

Re: Finding the insertion point in a list

2007-03-16 Thread kyosohma
On Mar 16, 12:59 pm, [EMAIL PROTECTED] wrote: > I have an ordered list e.g. x = [0, 100, 200, 1000], and given any > positive integer y, I want to determine its appropriate position in > the list (i.e the point at which I would have to insert it in order to > keep the list sorted. I can clearly do

Re: Finding the insertion point in a list

2007-03-16 Thread Matimus
You might look at the bisect module (part of the standard distribution). -- http://mail.python.org/mailman/listinfo/python-list

Finding the insertion point in a list

2007-03-16 Thread tkpmep
I have an ordered list e.g. x = [0, 100, 200, 1000], and given any positive integer y, I want to determine its appropriate position in the list (i.e the point at which I would have to insert it in order to keep the list sorted. I can clearly do this with a series of if statements: if yx[i] for i i