这就是我所做的,但这只在列表中找到一个项目我想打印出项目 36 的两个索引如果我发布这个的方式或我的问题不清楚,请帮助这是我到目前为止所做的我道歉提前[代码]
def main():
mylist=[]
for i in range(20):
mylist.append(i*3)
mylist.append(36)
mylist.sort()
print mylist
binarySearch(mylist,0,len(mylist),36)
def binarySearch(thelist,lower,upper,item):
if upper<lower:
print 'item not in the list'
return
middle=(lower+upper)/2
if thelist[middle]<item:
lower=middle+1
binarySearch(thelist,lower,upper,item)
elif thelist[middle]>item:
upper=middle-1
binarySearch(thelist,lower,upper,item)
else:
print 'the item was found at index ',thelist[middle],middle
return
main()