Python中的索引超出范围误差(IndexError:列表索引之外)



我似乎找不到我已经摆脱范围问题的问题:

layerZ = [layer_1,layer_2,layer_3,layer_4,layer_5,layer_6,layer_7,layer_8,layer_9,layer_10,layer_11,layer_12,layer_13]
    for x in range(0, 12):
        layerZ_total = [np.size(layerZ[x])]
        layerZ_sp = [np.count_nonzero(layerZ[x]==0)]
        layerZ_nonSp = [np.count_nonzero(layerZ[x])]
        #Printing the results on scree to trace
        print "Layer:",x+1,"Threshhold:",repr(ths),"Total Parameters: ",layerZ_totParam[x],"# Sp: ",layerZ_sp[x],"# Remained : ",layerZ_nonSp[x],"Sp %: ",float(layerZ_sp[x])/layerZ_total[x]

这应该有所帮助:

layerZ = [layer_1,layer_2,layer_3,layer_4,layer_5,layer_6,layer_7,layer_8,layer_9,layer_10,layer_11,layer_12,layer_13]
layerZ_total = []
layerZ_sp = []
layerZ_nonSp = []

for x in range(0, 12):
    layerZ_total.append(np.size(layerZ[x]))
    layerZ_sp.append(np.count_nonzero(layerZ[x]==0))
    layerZ_nonSp.append(np.count_nonzero(layerZ[x]))
    #Printing the results on scree to trace
    print "Layer:",x+1,"Threshhold:",repr(ths),"Total Parameters: ",layerZ_total[x],"# Sp: ",layerZ_sp[x],"# Remained : ",layerZ_nonSp[x],"Sp %: ",float(layerZ_sp[x])/layerZ_total[x]

在您的代码中,列表 layerZ_totallayerZ_splayerZ_nonSp在循环内每次都被重新定位,因此在循环中每次都有一个值,因此,当您尝试访问某些x index时,在它们内部仅包含一个index 0处的一个元素,list index out of range错误弹出。p>,如果您要在layerZ列表的每个layer_*上进行处理(取决于您的要求),则需要使用range(0, 13),就像这样做之后,x将对0进行CC_11,从而处理layerZ中的每个layer列表

相关内容

  • 没有找到相关文章

最新更新