开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
文件
master
分支 (1)
master
master
分支 (1)
master
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
master
分支 (1)
master
python3.7.4
/
Doc
/
library
/
bisect.rst
python3.7.4
/
Doc
/
library
/
bisect.rst
bisect.rst 5.27 KB
一键复制 编辑 原始数据 按行查看 历史
zhangweibo 提交于 2021年11月17日 13:49 +08:00 . git init

:mod:`bisect` --- Array bisection algorithm

.. module:: bisect
 :synopsis: Array bisection algorithms for binary searching.
.. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org>
.. sectionauthor:: Raymond Hettinger <python at rcn.com>

Source code: :source:`Lib/bisect.py`


This module provides support for maintaining a list in sorted order without having to sort the list after each insertion. For long lists of items with expensive comparison operations, this can be an improvement over the more common approach. The module is called :mod:`bisect` because it uses a basic bisection algorithm to do its work. The source code may be most useful as a working example of the algorithm (the boundary conditions are already right!).

The following functions are provided:

.. function:: bisect_left(a, x, lo=0, hi=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.

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

 Similar to :func:`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.

.. function:: insort_left(a, x, lo=0, hi=len(a))

 Insert *x* in *a* in sorted order. This is equivalent to
 ``a.insert(bisect.bisect_left(a, x, lo, hi), x)`` assuming that *a* is
 already sorted. Keep in mind that the O(log n) search is dominated by
 the slow O(n) insertion step.

.. function:: insort_right(a, x, lo=0, hi=len(a))
 insort(a, x, lo=0, hi=len(a))

 Similar to :func:`insort_left`, but inserting *x* in *a* after any existing
 entries of *x*.

.. seealso::

 `SortedCollection recipe
 <https://code.activestate.com/recipes/577197-sortedcollection/>`_ that uses
 bisect to build a full-featured collection class with straight-forward search
 methods and support for a key-function. The keys are precomputed to save
 unnecessary calls to the key function during searches.


Searching Sorted Lists

The above :func:`bisect` functions are useful for finding insertion points but can be tricky or awkward to use for common searching tasks. The following five functions show how to transform them into the standard lookups for sorted lists:

def index(a, x):
 'Locate the leftmost value exactly equal to x'
 i = bisect_left(a, x)
 if i != len(a) and a[i] == x:
 return i
 raise ValueError

def find_lt(a, x):
 'Find rightmost value less than x'
 i = bisect_left(a, x)
 if i:
 return a[i-1]
 raise ValueError

def find_le(a, x):
 'Find rightmost value less than or equal to x'
 i = bisect_right(a, x)
 if i:
 return a[i-1]
 raise ValueError

def find_gt(a, x):
 'Find leftmost value greater than x'
 i = bisect_right(a, x)
 if i != len(a):
 return a[i]
 raise ValueError

def find_ge(a, x):
 'Find leftmost item greater than or equal to x'
 i = bisect_left(a, x)
 if i != len(a):
 return a[i]
 raise ValueError

Other Examples

The :func:`bisect` function can be useful for numeric table lookups. This example uses :func:`bisect` to look up a letter grade for an exam score (say) based on a set of ordered numeric breakpoints: 90 and up is an 'A', 80 to 89 is a 'B', and so on:

>>> def grade(score, breakpoints=[60, 70, 80, 90], grades='FDCBA'):
... i = bisect(breakpoints, score)
... return grades[i]
...
>>> [grade(score) for score in [33, 99, 77, 70, 89, 90, 100]]
['F', 'A', 'C', 'C', 'B', 'A', 'A']

Unlike the :func:`sorted` function, it does not make sense for the :func:`bisect` functions to have key or reversed arguments because that would lead to an inefficient design (successive calls to bisect functions would not "remember" all of the previous key lookups).

Instead, it is better to search a list of precomputed keys to find the index of the record in question:

>>> data = [('red', 5), ('blue', 1), ('yellow', 8), ('black', 0)]
>>> data.sort(key=lambda r: r[1])
>>> keys = [r[1] for r in data] # precomputed list of keys
>>> data[bisect_left(keys, 0)]
('black', 0)
>>> data[bisect_left(keys, 1)]
('blue', 1)
>>> data[bisect_left(keys, 5)]
('red', 5)
>>> data[bisect_left(keys, 8)]
('yellow', 8)
Loading...
举报
举报成功
我们将于2个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

简介

暂无描述
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/python_sourcecode/python3.7.4.git
git@gitee.com:python_sourcecode/python3.7.4.git
python_sourcecode
python3.7.4
python3.7.4
master
点此查找更多帮助

搜索帮助

评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

AltStyle によって変換されたページ (->オリジナル) /