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 )

  

目录
相关文章
|
10月前
|
索引
bisect_left,bisect_right,bisect的用法,区别以及源码分析
bisect_left,bisect_right,bisect的用法,区别和源码分析
244 0
bisect_left,bisect_right,bisect的用法,区别以及源码分析
|
存储 C++
【PAT甲级 - C++题解】1115 Counting Nodes in a Binary Search Tree
【PAT甲级 - C++题解】1115 Counting Nodes in a Binary Search Tree
110 0
LeetCode 105. Construct Binary Tree
给定一颗二叉树的前序和顺序遍历,构造原二叉树。 注意:您可以假设树中不存在重复项。
81 0
LeetCode 105. Construct Binary Tree
LeetCode 106. Construct Binary Tree
给定一颗二叉树的中序和后续遍历,构造原二叉树。 注意:您可以假设树中不存在重复项。
104 0
LeetCode 106. Construct Binary Tree
|
Python
[Leetcode][python]Unique Binary Search Trees/不同的二叉查找树
题目大意 给出一个n,求1-n能够得到的所有二叉搜索树
109 0
Leetcode-Easy 72. Edit Distance
Leetcode-Easy 72. Edit Distance
97 0
Leetcode-Easy 72. Edit Distance
Data Structures and Algorithms (English) - 6-4 Reverse Linked List(20 分)
Data Structures and Algorithms (English) - 6-4 Reverse Linked List(20 分)
129 0
|
Java 索引
Go+ for range遍历
我们如果使用for遍历的话总觉得代码有点臃肿,不太雅观,这个时候我们可以使用for range来遍历,我们常用它来遍历数组、切片、字符串、map、以及channel。
152 0
|
Python
Python零基础学习笔记(三十二)—— list/tuple/dict/set文件操作
vimport pickle #数据持久性模块 Mylist = [1, 2, 3, 44, "aaa", True] path = r"C:\Users\Administrator\PycharmProjects\untitled\day011\文件读写\file2.
1187 0