微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在间隔内遍历txt文件; Python TypeError:“ int”对象不可下标

如何解决在间隔内遍历txt文件; Python TypeError:“ int”对象不可下标

我有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,1)
     20 
     21 plt.scatter(x,y)

<ipython-input-33-1eff7c030470> in scatterbisect(txtfile,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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。