Optimized method of cutting/slicing sorted lists

前端 未结 5 696
無奈伤痛
無奈伤痛 2021-01-02 08:10

Is there any pre-made optimized tool/library in Python to cut/slice lists for values \"less than\" something?

Here\'s the issue: Let\'s say I have a list like:

5条回答
  •  醉酒成梦
    2021-01-02 08:52

    Adding to Jon's answer, if you need to actually delete the elements less than 6 and want to keep the same reference to the list, rather than returning a new one.

    del a[:bisect.bisect_right(a,6)]
    

    You should note as well that bisect will only work on a sorted list.

提交回复
热议问题