Python代码IndexError: list assignment index out of range

问题描述 投票:0回答:0

我有密码:

pos = microscope.position
print("Current position: " + json.dumps(pos))
print("Homing")
pos['x'] = 0
pos['y'] = 0
pos['z'] = 0

# units in microns
xyStep = 1
xRange = 400
yRange = 400
zRange = 400
#zBase = maxPosition - micronToStep(zRange/2)
#zBase = -((30000 - 15000)/2)
zBase = -1000

saveData = {
    "xyStep": xyStep,
    "xRange": xRange,
    "yRange": yRange,
    "zRange": zRange,
    "zBase": zBase,
    "data": [[0] * ceil(xRange/xyStep) for i in range(ceil(yRange/xyStep))],
}

measurements = 1
pos = microscope.position
print("Current position: " + json.dumps(pos))

starting_pos = microscope.position
#figure(figsize=(20,5))

ix = 0
iy = 0

totalElapsed = 0
stepsTaken = 1
maxSteps = (xRange/xyStep) * (yRange/xyStep)
up = True

ix = 0
for x in range(-micronToStep(xRange/2), micronToStep(xRange/2) - micronToStep(xyStep), micronToStep(xyStep)):
    iy = 0
    for y in range(-micronToStep(yRange/2), micronToStep(yRange/2) - micronToStep(xyStep), micronToStep(xyStep)):
        start = timer()
        values = []
        if (up):
            pos = {}
            pos['x'] = x + 7500 # declination
            pos['y'] = y - 5500 # declination
            pos['z'] = zBase - micronToStep(zRange/2)
            pos['z2'] = zBase + micronToStep(zRange/2)
            print("Scanning up to position:", pos)

            values = microscope.measureZ(microscope, pos)
            values = np.asarray(values)
            thisTime =  timer() - start
            totalElapsed += thisTime
            up = False
        else:
            pos = {}
            pos['x'] = x + 7500 # declination
            pos['y'] = y - 5500 # declination
            pos['z'] = zBase + micronToStep(zRange/2)
            pos['z2'] = zBase - micronToStep(zRange/2)
            print("Scanning to position:", pos)

            values = microscope.measureZ(microscope, pos)
            values = np.asarray(values)
            values = np.flip(values)
            thisTime =  timer() - start
            totalElapsed += thisTime
            up = True


        print(pos)
        saveData['data'][ix][iy] = values

        iy += 1
        timeLeft = (maxSteps - stepsTaken) * (totalElapsed / stepsTaken) / 60 / 60
        print(">>>>>>>>>>>> Estimated time left: [" + str(timeLeft) + "hr], Per Scan: [" + str(totalElapsed / stepsTaken) + "s][" + str(thisTime) + "], Total Elapsed: [" + str(totalElapsed) + "s], Scans taken: [" + str(stepsTaken) + "] out of: [" + str(maxSteps) + "]")
        stepsTaken += 1
        pickle.dump(saveData, open('saveData.bin', 'wb'))

    ix += 1



pickle.dump(saveData, open('saveData.bin', 'wb'))

它适用于 xyStep = 10,但是当我将此参数更改为 xyStep = 1 时,在打印 Scans taken: [200] 之后,我得到一个错误:

in line 149, saveData['data'][ix][iy] = values: IndexError: list assignment index out of range
。函数 micronToStep 的定义很简单: def micronToStep(v): return round((v * 1000)/62).

我假设问题出在数组维度上。更准确地说,如果需要解决此问题,我可以添加有关代码的更多信息。

如何纠正这个问题?

python arrays list multidimensional-array
© www.soinside.com 2019 - 2024. All rights reserved.