import bisect

L = [1,3,3,5,7,9]

x = 30
##在L中查找x,x存在时返回x左侧的位置 x不存在返回应该插入的位置
x_insert_point = bisect.bisect_left(L,x)
print(x_insert_point)

#在L中查找x,x存在时返回x右侧的位置,x不存在返回应该插入的位置
x_insert_point = bisect.bisect_right(L,x)
print(x_insert_point)

#将x插入到列表L中,x存在时插入在左侧
x_insert_left = bisect.insort_left(L,x)
print(L)
#将x插入到列表L中,x存在时插入在右侧
x_insert_left = bisect.insort_right(L,x)
print(L)

 

相关文章:

  • 2021-10-25
  • 2022-12-23
  • 2021-07-07
  • 2021-10-23
  • 2022-12-23
  • 2021-11-25
  • 2021-11-02
  • 2022-12-23
猜你喜欢
  • 2022-03-08
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-30
  • 2022-03-03
相关资源
相似解决方案