
Untitled
By: a guest on
May 28th, 2012 | syntax:
None | size: 0.87 KB | hits: 14 | expires: Never
run for loop limited iterations in python
low = 0
high = len(sortedObjects)
while low < high:
mid = (low + high)/2
if sortedObjects[mid].attr < desired_attr:
low = mid + 1
elif sortedSamples[mid].attr > desired_attr:
high = mid
else:
newList.append(sortedObjects[mid])
break
else:
# Find the first element that matches
while mid > 0 and sortedSamples[mid - 1].attr == desired_attr:
mid -= 1
# Iterate until an element that doesn't match is found.
while mid < len(sortedSamples) and sortedSamples[mid].attr == desired_attr:
newList.append(sortedObjects[mid])
mid += 1
attr_list = [o.attr for o in sortedObjects]
import bisect
left_i = bisect.bisect_left(attr_list, desired_attr)
right_i = bisect.bisect_right(attr_list, desired_attr, left_i)
newList = sortedObjects[left_i:right_i]