Return first index of value in sorted-key list. Raise ValueError if `value` is not present. Index must be between `start` and `stop` for the `value` to be considered present. The default value, None, for `start` and `stop` indicate the beginning and end of the sorte
(self, value, start=None, stop=None)
| 2412 | |
| 2413 | |
| 2414 | def index(self, value, start=None, stop=None): |
| 2415 | """Return first index of value in sorted-key list. |
| 2416 | |
| 2417 | Raise ValueError if `value` is not present. |
| 2418 | |
| 2419 | Index must be between `start` and `stop` for the `value` to be |
| 2420 | considered present. The default value, None, for `start` and `stop` |
| 2421 | indicate the beginning and end of the sorted-key list. |
| 2422 | |
| 2423 | Negative indices are supported. |
| 2424 | |
| 2425 | Runtime complexity: `O(log(n))` -- approximate. |
| 2426 | |
| 2427 | >>> from operator import neg |
| 2428 | >>> skl = SortedKeyList([5, 4, 3, 2, 1], key=neg) |
| 2429 | >>> skl.index(2) |
| 2430 | 3 |
| 2431 | >>> skl.index(0) |
| 2432 | Traceback (most recent call last): |
| 2433 | ... |
| 2434 | ValueError: 0 is not in list |
| 2435 | |
| 2436 | :param value: value in sorted-key list |
| 2437 | :param int start: start index (default None, start of sorted-key list) |
| 2438 | :param int stop: stop index (default None, end of sorted-key list) |
| 2439 | :return: index of value |
| 2440 | :raises ValueError: if value is not present |
| 2441 | |
| 2442 | """ |
| 2443 | _len = self._len |
| 2444 | |
| 2445 | if not _len: |
| 2446 | raise ValueError('{0!r} is not in list'.format(value)) |
| 2447 | |
| 2448 | if start is None: |
| 2449 | start = 0 |
| 2450 | if start < 0: |
| 2451 | start += _len |
| 2452 | if start < 0: |
| 2453 | start = 0 |
| 2454 | |
| 2455 | if stop is None: |
| 2456 | stop = _len |
| 2457 | if stop < 0: |
| 2458 | stop += _len |
| 2459 | if stop > _len: |
| 2460 | stop = _len |
| 2461 | |
| 2462 | if stop <= start: |
| 2463 | raise ValueError('{0!r} is not in list'.format(value)) |
| 2464 | |
| 2465 | _maxes = self._maxes |
| 2466 | key = self._key(value) |
| 2467 | pos = bisect_left(_maxes, key) |
| 2468 | |
| 2469 | if pos == len(_maxes): |
| 2470 | raise ValueError('{0!r} is not in list'.format(value)) |
| 2471 |