11

当我使用该bisect_left()功能时,为什么我没有得到index元素的,而是index + 1

import bisect

t3 = ['carver', 'carvers', 'carves', 'carving', 'carvings']
print bisect.bisect(t3, 'carves')    # 3
print bisect.bisect(t3, 'carving')   # 4
print bisect.bisect(t3, 'carver')    # 1
4

1 回答 1

25

bisect.bisect()是 的简称bisect.bisect_right()不是 bisect.bisect_left()

您需要使用全名 , bisect.bisect_left(),而不是:

>>> import bisect
>>> t3 = ['carver', 'carvers', 'carves', 'carving', 'carvings']
>>> bisect.bisect(t3, 'carves')
3
>>> bisect.bisect_left(t3, 'carves')
2
>>> bisect.bisect == bisect.bisect_right
True
>>> bisect.bisect == bisect.bisect_left
False
于 2013-10-04T13:21:46.870 回答