Bisect, is it possible to work with descending sorted lists?

前端 未结 10 737
旧巷少年郎
旧巷少年郎 2020-12-09 15:37

How can I use bisect module on lists that are sorted descending? e.g.

import bisect

x = [1.0,2.0,3.0,4.0] # normal, ascending
bisect.insort(x,2.5)  # -->         


        
10条回答
  •  半阙折子戏
    2020-12-09 16:13

    I've never used to the bisect package. But if it only works in ascending order and you're always keeping your list sorted (whether ascending or descending) then you could simply sort beforehand and then invert (if you want to keep it descending).

    x.sort()
    bisect.insort(x,2.5)
    x.reverse()
    

    Obviously more a hack then a real solution but at least it would work.

提交回复
热议问题