Finding the insertion point in a list
Steve Holden
steve at holdenweb.com
Sat Mar 17 06:46:17 EDT 2007
Paul Rubin wrote:
> Steven D'Aprano <steve at REMOVE.THIS.cybersource.com.au> 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 solution (like the bisect one suggested almost as soon as
this thread started) that doesn't iterate over the whole list.
Having said which, for the promoted use cases I agree that the append()
and sort() paradigm wins hands down until it starts to make a real and
observable difference to the run time.
regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Recent Ramblings http://holdenweb.blogspot.com
More information about the Python-list
mailing list