searching a list of tuples

Jason Orendorff jason at jorendorff.com
Wed Feb 13 22:30:51 CET 2002


Marcus Stojek writes:
> I have a (huge) list of tuples :
> list=(float 1,float 2,float 3,int).
> 
> The list has been sorted with list.sort()
> Now I have to slice the list in a way
> that:
> 
>   a <= float 1 <= b

Well, on my computer I can make a float that
represents "infinity".  So this works:

import bisect

INF = 1e600
left = bisect.bisect_right(mylist, (a, -INF, -INF, -INF))
right = bisect.bisect_left(mylist, (b, INF, INF, INF))
myslice = mylist[left:right]

## Jason Orendorff    http://www.jorendorff.com/




More information about the Python-list mailing list