Python 类型错误 : 'List' object is not callable

标签 python list

我在 Python27 的这段小代码内容中遇到了这个错误。谁能帮我这个?提前致谢。

Run Time Error Traceback (most recent call last): File "5eb4481881d51d6ece1c375c80f5e509.py", line 57, in print len(arr) TypeError: 'list' object is not callable

global maximum

def _lis(arr , n ):

    # to allow the access of global variable
    global maximum

    # Base Case
    if n == 1 :
        return 1

    # maxEndingHere is the length of LIS ending with arr[n-1]
    maxEndingHere = 1

    """Recursively get all LIS ending with arr[0], arr[1]..arr[n-2]
       IF arr[n-1] is maller than arr[n-1], and max ending with
       arr[n-1] needs to be updated, then update it"""
    for i in xrange(1, n):
        res = _lis(arr , i)
        if arr[i-1] < arr[n-1] and res+1 > maxEndingHere:
            maxEndingHere = res +1

    # Compare maxEndingHere with overall maximum. And
    # update the overall maximum if needed
    maximum = max(maximum , maxEndingHere)

    return maxEndingHere

def lis(arr):

    # to allow the access of global variable
    global maximum

    # lenght of arr
    n = len(arr)

    # maximum variable holds the result
    maximum = 1

    # The function _lis() stores its result in maximum
    _lis(arr , n)

    return maximum

num_t = input()

len = [None]*num_t

arr = []

for i in range(0,num_t):

    len[i] = input()

    arr.append(map(int, raw_input().split()))

    print len(arr)
    break    

最佳答案

您已经创建了一个名为 len 的列表,您可以从这里看到,您可以对其进行索引:

len[i] = input()

很自然地,len 不再是获取列表长度的函数,从而导致您收到错误。

解决方案:将您的 len 列表命名为其他内容。

关于Python 类型错误 : 'List' object is not callable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39971037/

相关文章:

python - 多特征线性回归的实现

python - 根据 Pandas 中一列的总和添加新列并按其他 2 列分组

python - 在 Python 的列表列表中查找特定项目

python - 如何在Python中比较两个列表中各自列表中相同索引中的元素?

python - 为什么在 Python 中使用 [e] * n(用于创建单个项目重复 n 次的列表)速度很快?

python - 将数字添加到嵌套列表中的整数

python - 替代WITH RECURSIVE 子句

python - 如何使用另一个字段的值向 pythonjsonlogger.jsonlogger.JsonFormatter 添加附加字段?

python - 使用 python 查找具有特定配置的 Cisco IOS 接口(interface)

python - 将值保存到列表中并将其发送到函数