包含元组的数组列表(?)

时间:2015-07-10 17:30:02

标签: python arrays numpy

我遇到了TypeError:列表索引必须是整数,而不是元组。但是,我无法弄清楚如何修复它,因为我显然误解了元组的位置(甚至不知道会有一个我理解的内容)。我的索引不应该和我传入的值都是整数吗?

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

1 个答案:

答案 0 :(得分: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][:,:,:]