pos = bisect_left(myList, myNumber)# 找到 mylist 里面第一个不比 mynumber 小(即 >= )的数的索引下标 # 返回的插入点 pos 可以将数组myList分成两部分。左侧是 all(val < x for val in myList[lo:i]) ,右侧是 all(val >= x for val in myList[i:hi]) before = myList[pos -1] after...
python:从整数列表(数组)中获取最接近给定值的数字---2020.12.9更新--- 简化了bisect_left def takeClosest(myList, myNumber):if (myNumber >= myList[-1]):return myList[-1]elif myNumber <= myList[0]:return myList[0]pos = bisect_left(myList, myNumber) # 找到 mylist ⾥⾯第...