Python lists/arrays: disable negative indexing wrap-around in slices

后端 未结 4 2050
不知归路
不知归路 2020-12-11 15:43

While I find the negative number wraparound (i.e. A[-2] indexing the second-to-last element) extremely useful in many cases, when it happens inside a slice it i

4条回答
  •  伪装坚强ぢ
    2020-12-11 16:17

    If this only needs to apply in a few specific operations, a simple & straightworward if index>=0: do_something(array[i]) / if index<0: raise IndexError would do.

    If this needs to apply wider, it's still the same logic, just being wrapped in this manner or another.

提交回复
热议问题