2015-07-10 93 views
0

我一直在遇到TypeError:list索引必須是整數,而不是元組。然而,我無法弄清楚如何解決這個問題,因爲我顯然誤解了這個元組的位置(甚至沒有意識到我會理解這個元素)。我的索引和傳遞的值不應該是整數嗎?包含元組的數組列表(?)

def videoVolume(images): 
    """ Create a video volume from the image list. 

    Note: Simple function to convert a list to a 4D numpy array. 

    Args: 
     images (list): A list of frames. Each element of the list contains a 
         numpy array of a colored image. You may assume that each 
         frame has the same shape, (rows, cols, 3). 

    Returns: 
     output (numpy.ndarray): A 4D numpy array. This array should have 
           dimensions (num_frames, rows, cols, 3) and 
           dtype np.uint8. 
    """ 
    output = np.zeros((len(images), images[0].shape[0], images[0].shape[1], 
         images[0].shape[2]), dtype=np.uint8) 

    # WRITE YOUR CODE HERE. 
    for x in range(len(images)): 
     output[:,:,:,:] = [x, images[x,:,3], images[:,x,3], 3] 



    # END OF FUNCTION. 
    return output 
+2

那是完整的代碼?還有什麼是追溯? –

+0

這是完整的代碼。 回溯: 回溯(最近通話最後一個): 文件 「assignment_test.py」,線路377,在 如果不是test_videoVolume(): 文件 「assignment9_test.py」,行73,在test_videoV usr_out =分配.videoVolume(img_list) 輸出[:,:,:,:] = [x,images [x,:,3]文件「C:\ Users \ user \ Documents \ assign.py」,第53行,videoVolume ,圖像[:,x,3 TypeError:列表索引必須是整數,而不是元組 – Runner

+0

'images [x,:,3]'是image [(x,:,3)]的縮寫。 '[]'裏面的東西是一個「元組」。 'np.array'可以處理那種索引,普通列表不能。 – hpaulj

回答

1

元組在錯誤消息被稱爲是在這裏的索引的x,:,3

images[x,:,3] 

發生這種情況的原因是,images傳遞在作爲列表幀(各一個3d numpy數組),但是你試圖訪問它,就好像它本身是一個numpy數組。 (試着做lst = [1, 2, 3]; lst[:,:],你會看到你得到相同的錯誤信息)。

相反,你的意思是訪問它,就像這樣images[x][:,:,:],例如

for x in range(len(images)): 
    output[x,:,:,:] = images[x][:,:,:] 
+0

這使得更多的意義,並修復我的問題,謝謝! – Runner

相關問題