Python列表作为列表索引

时间:2019-12-09 22:52:08

标签: python arrays python-3.x numpy

真的不确定要问正确的问题,但是是否可以将列表作为列表的索引?

例如:

pixelAddr=[50,50] # list
img[pixelAddr[0], pixelAddr[1]]=[255,255,255] # This is the way I know

# Is something like this possible? I get syntax errors when I try it... 
img[*pixelAddr]=[255,255,255]

顺便说一句,使用python 3.7

1 个答案:

答案 0 :(得分:1)

当您进行操作时:img[pixelAddr[0], pixelAddr[1]]实际上只是将索引重新打包为 tuple ,因此,这实际上就是您所需要的:

pixelAddr=(50,50) # NOTE THESE ARE ROUND PARENTHASIS
img[pixelAddr]=[255,255,255]

# or 
pixelAddrList = [50,50]
img[tuple(pixelAddr)]=[255,255,255]