python运行时出现“TypeError: 'map' object is not subscriptable”错误

部分代码
def main(infile,outfile):
#读取输入图像文件,并建立RGB数组
ifp=Image.Image
ifp=Image.open(infile)
width,height=ifp.size
matrixR=map(lambda _ : [0]*(height),range(width))#height行,width列
matrixG=map(lambda _ : [0]*(height),range(width))
matrixB=map(lambda _ : [0]*(height),range(width))
for x in range(width):
for y in range(height):
matrixR[x][y]=ifp.getpixel((x,y))[0]
matrixG[x][y]=ifp.getpixel((x,y))[1]
matrixB[x][y]=ifp.getpixel((x,y))[2]

有问题就google,一般能找到解决的办法。
map() doesn't return a list, it returns a map object.
You need to call list(map) if you want it to be a list again.
原文:http://stackoverflow.com/questions/6800481/python-map-object-is-not-subscriptable
温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-12-22
试试matrixR = tuple(matrixR)