【问题标题】:TypeError in python function (int object not subscriptable)python函数中的TypeError(int对象不可下标)
【发布时间】:2017-06-27 02:36:17
【问题描述】:

我有一个函数可以打印出 3x3 矩阵的 2x2 矩阵卷积:

image = [[1,0,1],       # Original image
        [0,1,0],
        [1,0,1]]

我的函数应该在哪里打印出来:

[1,0]
[0,1]
[0,1]
[1,0]
[0,1]
[1,0]
[1,0]
[0,1]

函数如下

def convolution(image,result):
    # Image being 2d matrix
    # Result being return stacked 2d matrices
    # DECLARE LOCAL VARIABLES
    a = 0   # Slice [a:b]
    b = 2
    r = 0
    # For row in image:
    for row in image:
        # While b < row length:
        while b < len(row):
            print(row[r][a:b])   # HERE IS THE ERROR
            print(row[r+1][a:b])
            a += 1
            b += 1
        a = 0   # Slice [a:b]
        b = 2
        matrix2d.clear()

我收到以下错误:

Traceback (most recent call last):
  File "conv.py", line 49, in <module>
    convolution(image,result3d)
  File "conv.py", line 24, in convolution
    print(row[r][a:b])
TypeError: 'int' object is not subscriptable

错误信息对我来说比较模棱两可。有什么办法可以纠正这个错误?

【问题讨论】:

    标签: python arrays matrix conv-neural-network convolution


    【解决方案1】:

    在您的代码中,row 是图像的一行,例如第一行是 [1,0,1]。然后在您的 while 循环中,row[r] 是一个整数,而不是一个数组。

    错误消息给你错误的行,并说你不能取整数的下标,这意味着如果aint,你不能做a[1]。有了这些信息,您就可以很好地发现row[r] 确实是一个整数。

    【讨论】:

      猜你喜欢
      • 2023-03-20
      • 2015-07-31
      • 2017-08-08
      • 2023-04-02
      • 2015-12-18
      • 2017-07-15
      • 2012-02-21
      • 2018-08-07
      相关资源
      最近更新 更多