In Python3, the classic mid = start + (end - start) / 2 throws TypeError: list indices must be integers or slices, not float
In Python3, the classic mid = start + (end - start) / 2 throws TypeError: list indices must be integers or slices, not float
在Python3,
经典二分查找第一步
mid = start + (end - start) / 2
抛出
TypeError: list indices must be integers or slices, not float
因为除法产生 float
而不是默认的 int
。
有没有比 int(mid)
更好的方法来处理这个问题?
在 Python 3 中,/
运算符进行浮点除法,即使是在两个整数之间。
您需要 //
,它将执行整数除法。
在Python3,
经典二分查找第一步
mid = start + (end - start) / 2
抛出
TypeError: list indices must be integers or slices, not float
因为除法产生 float
而不是默认的 int
。
有没有比 int(mid)
更好的方法来处理这个问题?
在 Python 3 中,/
运算符进行浮点除法,即使是在两个整数之间。
您需要 //
,它将执行整数除法。