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

前端 未结 10 709
旧巷少年郎
旧巷少年郎 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:22

    You can insert like this

    bisect.insort_left(lists, -x)

    and extract elements like this

    value = - lists[i]
    

提交回复
热议问题