Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 28th, 2012  |  syntax: None  |  size: 0.87 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. run for loop limited iterations in python
  2. low   = 0
  3.   high  = len(sortedObjects)
  4.   while low < high:
  5.     mid = (low + high)/2
  6.     if sortedObjects[mid].attr < desired_attr:
  7.       low = mid + 1
  8.     elif sortedSamples[mid].attr > desired_attr:
  9.       high = mid
  10.     else:
  11.       newList.append(sortedObjects[mid])
  12.       break
  13.        
  14. else:
  15.     # Find the first element that matches
  16.     while mid > 0 and sortedSamples[mid - 1].attr == desired_attr:
  17.         mid -= 1
  18.  
  19.     # Iterate until an element that doesn't match is found.
  20.     while mid < len(sortedSamples) and sortedSamples[mid].attr == desired_attr:
  21.         newList.append(sortedObjects[mid])
  22.         mid += 1
  23.        
  24. attr_list = [o.attr for o in sortedObjects]
  25.        
  26. import bisect
  27. left_i = bisect.bisect_left(attr_list, desired_attr)
  28. right_i = bisect.bisect_right(attr_list, desired_attr, left_i)
  29. newList = sortedObjects[left_i:right_i]