美文网首页
Python bisect 笔记

Python bisect 笔记

作者: KkevinZz | 来源:发表于2017-08-30 02:08 被阅读0次

bisect.bisect_left(axlo=0hi=len(a))

Locate the insertion point for x in a to maintain sorted order. The parameters lo and hi may be used to specify a subset of the list which should be considered; by default the entire list is used. If x is already present in a, the insertion point will be before (to the left of) any existing entries. The return value is suitable for use as the first parameter to list.insert() assuming that a is already sorted.

The returned insertion point i partitions the array a into two halves so that all(val < x for val in a[lo:i]) for the left side and all(val >= x for val in a[i:hi])for the right side.

bisect.bisect_right(axlo=0hi=len(a))bisect.bisect(axlo=0hi=len(a))

Similar to bisect_left(), but returns an insertion point which comes after (to the right of) any existing entries of x in a.

The returned insertion point i partitions the array a into two halves so that all(val <= x for val in a[lo:i]) for the left side and all(val > x for val in a[i:hi])for the right side.

```python

import bisect

print(bisect.bisect_left(ls,0))

寻找 elem出现的最左位置,或者寻找第一个比x大的index

example:

ls = [-1,1,1,1,1,1,1,2,3,4,5]

寻找第一个比0大的数字

bisect_left(ls,0)

寻找1第一次出现的地方

bisect_left(ls,1)

right同理

print(bisect.bisect_right(ls,1))

```

相关文章

网友评论

      本文标题:Python bisect 笔记

      本文链接:https://www.haomeiwen.com/subject/ypwnbttx.html