I have an extremely large list of objects and I need to find all that have the identical attribute (any_object.any_attribute) and then append them to a new list. So I have pre-sorted them and run a binary search algo. I have found the object with the matching attribute but the problem is that there are more than one such objects (they are neighbours) but I can't figure out a clean way of running a loop on these contiguous objects so that they all can be appended. My code is pasted below.
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
So I need to write some new code in the last else block which would iterate over all the objects with the same attributes and append them. Sounds like a for loop would be needed but is it possible to run a for loop for limited iterations, like in C?
I do not want to iterate over the whole list as that would be slower and one of the requirements of this script is that it has to be fast and efficient. It will be run on really large sets of data and we are looking at execution times of 10-12 hrs. Thanks in advance!