栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python 内置bisect(对分) 模块 排序: 有序序列的查找和插入

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python 内置bisect(对分) 模块 排序: 有序序列的查找和插入

# 意思是: 将元素 a 插入到s1序列中, 返回应当插入位置的索引 In [5]: bisect.bisect_left(s1, h ) Out[5]: 2 # 意思是: 将元素 h 插入到s1序列中, 返回左侧插入位置的索引 In [6]: bisect.bisect_right(s1, h ) Out[6]: 3 # 意思是: 将元素 h 插入到s1序列中, 返回右则插入位置的索引 In [7]: bisect.insort(s1, h ) # 意思是: 将元素 h 插入到s1序列中 In [8]: bisect.insort(s1, z ) In [9]: s1 Out[9]: [ a , b , h , h , z ] In [10]: bisect.insort_left(s1, y ) In [10]: s1 Out[10]: [ a , b , h , h , y , z ] In [11]: bisect.insort_right(s1, y1 ) In [12]: s1 Out[12]: [ a , b , h , h , y , y1 , z ] 小应用场景
import bisect
def get_score_level(score, scores (60, 70, 80, 90), grades EDCBA ):
 position bisect.bisect(scores, score)
 return grades[position]
print(get_score_level(65))

可以不用那么一推if else了

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/267778.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号