Python维护值列表

Python维护值列表,python,list,python-2.7,linked-list,Python,List,Python 2.7,Linked List,我正在尝试使用3个特定列表(peak、X3和Y3)制作彩色地图。我在使用insert函数构建的地方编写的列表代码。代码如下所示: for k in range(112): if (act_info.nb_act[k]) >0: max_V.insert(k,np.max(array_data[:,k]))#find the maximum voltage for the set of samples of each channel min_V.inser

我正在尝试使用3个特定列表(peak、X3和Y3)制作彩色地图。我在使用insert函数构建的地方编写的列表代码。代码如下所示:

for k in range(112):

   if (act_info.nb_act[k]) >0:
       max_V.insert(k,np.max(array_data[:,k]))#find the maximum voltage for the set of samples of each channel
       min_V.insert(k,np.min(array_data[:,k]))                     #find the minimum voltage for the set of samples of each channe
       dif = ((((max_V[k] - min_V[k])/(4096.0))*10.0)/elec_array.chan[k].gain)*1000
       peak.insert(k,dif)  #then compute the difference between the max and min to find the peak-to-peak value
       X3.insert(k, elec_array.chan[k].x)                          #get the coordinates
       Y3.insert(k, elec_array.chan[k].y)

    else:

        Z2as[k] = k
        aa=aa+1
我的目的是在act_info.b_act[k]不大于零时获取所有3个列表并记录。 当我尝试运行此代码时,会出现以下错误:

Traceback (most recent call last):
  File "/Users/AhmedNiri/Ahmed/2D_Mapping_Program_V8.py", line 433, in on_scar_button
    self.scar_map(file_info, elec_array, aux_elec, act_info, act, act_type, array_data)
  File "/Users/AhmedNiri/Ahmed/2D_Mapping_Program_V8.py", line 911, in scar_map
    dif = ((((max_V[k] - min_V[k])/(4096.0))*10.0)/elec_array.chan[k].gain)*1000
IndexError: list index out of range
似乎当act_info.b_act[k]小于0时,它进入else语句记录其小于0,然后似乎中断了列表生成,因此给出了此错误。我使用python的时间不长,不知道如何解决这个问题


提前感谢您的帮助:)。

堆栈跟踪会准确地告诉您刚才所说的内容。指数k超出范围。它试图调用数据中不存在的东西(列表、字典或元组等)。为了避免程序崩溃,请使用
try:except:
块包装if条件

for k in range(112):

  try:

     if (act_info.nb_act[k]) >0:
         max_V.insert(k,np.max(array_data[:,k]))#find the maximum voltage for the set of samples of each channel
         min_V.insert(k,np.min(array_data[:,k]))                     #find the minimum voltage for the set of samples of each channe
         dif = ((((max_V[k] - min_V[k])/(4096.0))*10.0)/elec_array.chan[k].gain)*1000
         peak.insert(k,dif)  #then compute the difference between the max and min to find the peak-to-peak value
         X3.insert(k, elec_array.chan[k].x)                          #get the coordinates
         Y3.insert(k, elec_array.chan[k].y)

    except IndexError:
      pass

它被设置为通过,所以如果这种情况再次发生,它不会使您的程序崩溃。您可以将其设置为关闭程序,或标记用户,然后忽略它,等等。

但在程序中断后,如何继续该序列。因为如果我理解正确,这段代码将通过异常,但是异常后的数字呢?因为它们似乎没有被使用,而我喜欢使用它们。请你详细说明一下,谢谢你的帮助@awbemaurera你在谈论else语句中的代码吗?不,我在谈论else之后,这些值似乎没有生成。这意味着我猜列表会在异常处停止,不会生成列表的其余部分。我想要列表的其余部分的原因是生成一张地图@奥贝莫勒