python标准库学习5 ---bisect — Array bisection algorithm

简介:
#coding=utf-8
 
import  bisect
 
list = [ 1 , 2 , 3 , 4 , 6 , 7 , 8 , 9 ]   #假定list已经排序
print  bisect.bisect_left( list , 5 #返回5应该插入的索引位置
 
print  bisect.bisect_right( list , 5 )
 
print  bisect.bisect( list , 5 )
 
bisect.insort_left( list , 5 , 0 , len ( list ))
print  list
 
bisect.insort_right( list , 5 )
print  list
 
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
 
>>> 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' ]
 
>>> 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 )

  

目录
相关文章
|
29天前
|
编解码 算法 程序员
【C++ 泛型编程 高级篇】 C++ 14 模版元编程 遍历元组 编译期生成整数序列 std::index_sequence和std::make_index_sequence 使用指南(三)
【C++ 泛型编程 高级篇】 C++ 14 模版元编程 遍历元组 编译期生成整数序列 std::index_sequence和std::make_index_sequence 使用指南
26 0
|
29天前
|
存储 编译器 程序员
【C++ 泛型编程 高级篇】 C++ 14 模版元编程 遍历元组 编译期生成整数序列 std::index_sequence和std::make_index_sequence 使用指南(一)
【C++ 泛型编程 高级篇】 C++ 14 模版元编程 遍历元组 编译期生成整数序列 std::index_sequence和std::make_index_sequence 使用指南
48 0
|
29天前
|
C++ 索引
【C++ 泛型编程 高级篇】 C++ 14 模版元编程 遍历元组 编译期生成整数序列 std::index_sequence和std::make_index_sequence 使用指南(二)
【C++ 泛型编程 高级篇】 C++ 14 模版元编程 遍历元组 编译期生成整数序列 std::index_sequence和std::make_index_sequence 使用指南
26 0
|
1月前
|
Python
Python中sort和sorted函数用法解析
Python中sort和sorted函数用法解析
18 0
|
9月前
|
Shell Python
#PY小贴士# 同样是排序,sort和sorted有啥不同?
Python 这么设计固然有它的道理(sorted 是后引入的,对 sort 的补充),但这并不是必然的选择。比如与 sorted 功能有些类似的,random 模块的 shuffle 方法,就是在原列表基础上打乱顺序,而非返回一个新列表
|
Go
一文掌握使用 Go 标准库 sort 对切片进行排序
本文介绍了如何使用 sort 包里的函数,对基本数据类型的切片进行排序。sort 包还提供了对自定义的集合进行排序,需要实现 Interface 接口,由使用者去自定义排序规则,通过 sort.Sort 函数进行排序。
117 1
一文掌握使用 Go 标准库 sort 对切片进行排序
LeetCode 415. Add Strings
给定两个字符串形式的非负整数 num1 和num2 ,计算它们的和。
65 0
LeetCode 415. Add Strings
|
机器学习/深度学习 人工智能
Collatz Problem----map妙用
题目描述 A sequence a={a1,a2,a3,…} is determined as follows: The first term s is given as input. Let f(n) be the following function: f(n)=n/2 if n is even, and f(n)=3n+1 if n is odd. ai=s when i=1, and ai=f(ai−1) when i>1. Find the minimum integer m that satisfies the following condition:
129 0
Leetcode-Easy 977. Squares of a Sorted Array
Leetcode-Easy 977. Squares of a Sorted Array
77 0
Leetcode-Easy21. Merge Two Sorted Lists
Leetcode-Easy21. Merge Two Sorted Lists
84 0
Leetcode-Easy21. Merge Two Sorted Lists