Return the index where to insert item x in list a, assuming a is sorted. The return value i is such that all e in a[:i] have e < x, and all e in a[i:] have e >= x. So if x already appears in the list, a.insert(i, x) will insert just before the leftmost x already there. Optional ar
(a, x, lo=0, hi=None, *, key=None)
| 72 | a.insert(lo, x) |
| 73 | |
| 74 | def bisect_left(a, x, lo=0, hi=None, *, key=None): |
| 75 | """Return the index where to insert item x in list a, assuming a is sorted. |
| 76 | |
| 77 | The return value i is such that all e in a[:i] have e < x, and all e in |
| 78 | a[i:] have e >= x. So if x already appears in the list, a.insert(i, x) will |
| 79 | insert just before the leftmost x already there. |
| 80 | |
| 81 | Optional args lo (default 0) and hi (default len(a)) bound the |
| 82 | slice of a to be searched. |
| 83 | |
| 84 | A custom key function can be supplied to customize the sort order. |
| 85 | """ |
| 86 | |
| 87 | if lo < 0: |
| 88 | raise ValueError('lo must be non-negative') |
| 89 | if hi is None: |
| 90 | hi = len(a) |
| 91 | # Note, the comparison uses "<" to match the |
| 92 | # __lt__() logic in list.sort() and in heapq. |
| 93 | if key is None: |
| 94 | while lo < hi: |
| 95 | mid = (lo + hi) // 2 |
| 96 | if a[mid] < x: |
| 97 | lo = mid + 1 |
| 98 | else: |
| 99 | hi = mid |
| 100 | else: |
| 101 | while lo < hi: |
| 102 | mid = (lo + hi) // 2 |
| 103 | if key(a[mid]) < x: |
| 104 | lo = mid + 1 |
| 105 | else: |
| 106 | hi = mid |
| 107 | return lo |
| 108 | |
| 109 | |
| 110 | # Overwrite above definitions with a fast C implementation |
no outgoing calls
searching dependent graphs…