我有txt文件,其中填充了一列时间值和一列潜在(V)值。我试图以t0 = 2小时和tf = 12小时来记录每1小时(时间步长)的V值。我之前从未见过此错误,也不知道它来自何处。任何优化代码的帮助将不胜感激。
def bisection(array,value):
'''Given an ``array`` , and given a ``value`` , returns an index j such that ``value`` is between array[j]
and array[j+1]. ``array`` must be monotonic increasing. j=-1 or j=len(array) is returned
to indicate that ``value`` is out of range below and above respectively.'''
n = len(array)
if (value < array[0]):
return -1
elif (value > array[n-1]):
return n
jl = 0 # Initialize lower
ju = n-1 # and upper limits.
while (ju-jl > 1): # If we are not yet done,
jm=(ju+jl) >> 1 # compute a midpoint with a bitshift
if (value >= array[jm]):
jl=jm # and replace either the lower limit
else:
ju=jm # or the upper limit, as appropriate.
# Repeat until the test condition is satisfied.
if (value == array[0]): # edge cases at bottom
return 0
elif (value == array[n-1]): # and top
return n-1
else:
return array[jl],jl
def scatterBisect(txtfile,start,end,interval):
t_vals = txtfile[:,0]
V_vals = txtfile[:,1]
t_points = []
V_points = []
t = bisection(t_vals,start)[0]
while t <= end:
i = bisection(t_vals,t)[1]
V_points.append(V_vals[i])
t = bisection(t_vals,t + interval)[0]
t_points.append(t)
return t_points, V_points
x,y = scatterBisect(H_M_5a_purple,2,12,1)
plt.scatter(x,y)
plt.show()
这是完整的错误回溯:
TypeError Traceback (most recent call last)
<ipython-input-33-1eff7c030470> in <module>()
17 return t_points, V_points
18
---> 19 x,y = scatterBisect(H_M_5a_purple,2,12,1)
20
21 plt.scatter(x,y)
<ipython-input-33-1eff7c030470> in scatterBisect(txtfile, start, end, interval)
11 i = bisection(t_vals,t)[1]
12 V_points.append(V_vals[i])
---> 13 t = bisection(t_vals,t + interval)[0]
14 t_points.append(t)
TypeError: 'int' object is not subscriptable