MCPcopy Create free account
hub / github.com/grantjenks/python-sortedcontainers / index

Method index

sortedcontainers/sortedlist.py:1379–1453  ·  view source on GitHub ↗

Return first index of value in sorted 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 sorted li

(self, value, start=None, stop=None)

Source from the content-addressed store, hash-verified

1377
1378
1379 def index(self, value, start=None, stop=None):
1380 """Return first index of value in sorted list.
1381
1382 Raise ValueError if `value` is not present.
1383
1384 Index must be between `start` and `stop` for the `value` to be
1385 considered present. The default value, None, for `start` and `stop`
1386 indicate the beginning and end of the sorted list.
1387
1388 Negative indices are supported.
1389
1390 Runtime complexity: `O(log(n))` -- approximate.
1391
1392 >>> sl = SortedList('abcde')
1393 >>> sl.index('d')
1394 3
1395 >>> sl.index('z')
1396 Traceback (most recent call last):
1397 ...
1398 ValueError: 'z' is not in list
1399
1400 :param value: value in sorted list
1401 :param int start: start index (default None, start of sorted list)
1402 :param int stop: stop index (default None, end of sorted list)
1403 :return: index of value
1404 :raises ValueError: if value is not present
1405
1406 """
1407 _len = self._len
1408
1409 if not _len:
1410 raise ValueError('{0!r} is not in list'.format(value))
1411
1412 if start is None:
1413 start = 0
1414 if start < 0:
1415 start += _len
1416 if start < 0:
1417 start = 0
1418
1419 if stop is None:
1420 stop = _len
1421 if stop < 0:
1422 stop += _len
1423 if stop > _len:
1424 stop = _len
1425
1426 if stop <= start:
1427 raise ValueError('{0!r} is not in list'.format(value))
1428
1429 _maxes = self._maxes
1430 pos_left = bisect_left(_maxes, value)
1431
1432 if pos_left == len(_maxes):
1433 raise ValueError('{0!r} is not in list'.format(value))
1434
1435 _lists = self._lists
1436 idx_left = bisect_left(_lists[pos_left], value)

Callers 9

test_indexFunction · 0.95
test_index_valueerror1Function · 0.95
test_index_valueerror2Function · 0.95
test_index_valueerror3Function · 0.95
test_index_valueerror4Function · 0.95
test_index_valueerror5Function · 0.95
test_index_valueerror6Function · 0.95
test_index_valueerror7Function · 0.95
stress_index2Function · 0.95

Calls 1

_locMethod · 0.95

Tested by 9

test_indexFunction · 0.76
test_index_valueerror1Function · 0.76
test_index_valueerror2Function · 0.76
test_index_valueerror3Function · 0.76
test_index_valueerror4Function · 0.76
test_index_valueerror5Function · 0.76
test_index_valueerror6Function · 0.76
test_index_valueerror7Function · 0.76
stress_index2Function · 0.76