python index 自己实现
1 l = [2,3,4,223,42,56,7,389,586,845,8,894,343,46,345,3556,23,233,45,25,78,456,785,576,344,6,34,563,] 2 l.sort() 3 print(l) 4 def find(l ,aim,key=0): 5 '''二分法找值 6 key的关键字是这个新序列的第一个元素在原序列的哪一个位置 7 mid_index 是新序列的的中值 8 ''' 9 mid_index =len(l)//2 10 if len(l)<=1 and l[0] != aim: 11 return "silly bi " 12 if l[mid_index]>aim: 13 return find(l[0:mid_index],aim,key=key) 14 elif l[mid_index]<aim: 15 return find(l[mid_index+1:],aim,key=mid_index+key+1) 16 elif l[mid_index] == aim: 17 return mid_index+key 18 19 print(find(l,363)) 20 print(l.index(343))