Python Glossary Русский

all Python 3bisectfunction

bisect.bisect_right()

position

Return the index at which to insert x into a sorted list keeping it sorted — after any equal entries. Alias of bisect().

Syntax

bisect.bisect_right(a, x, lo=0, hi=len(a))

Nothing here validates the input — on an unsorted list you silently get a meaningless index. The difference from bisect_left shows up only among equal entries: use bisect_left to test membership, and bisect_right when a value sitting exactly on a boundary should fall into the upper range. Since Python 3.10 there is a key parameter, but it is applied to list elements only, so x must already be passed as a key.

Examples

import bisect
a = [1, 3, 3, 5, 7]
bisect.bisect_right(a, 3) # → 3 (после равных)
bisect.bisect_right(a, 0) # → 0
bisect.bisect_right(a, 3) - bisect.bisect_left(a, 3) # → 2 (сколько троек)

See also

Open in the full glossarydocs.python.orgReport an inaccuracy