【问题标题】:Python Type Error: 'List' object is not callablePython 类型错误:“列表”对象不可调用
【发布时间】:2017-02-19 15:04:57
【问题描述】:

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

运行时错误回溯(最后一次调用):文件 “5eb4481881d51d6ece1c375c80f5e509.py”,第 57 行,在 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 函数。尝试使用另一个变量名。
  • @AndrewL。谢谢!刚刚想通了
  • @acw1668 谢谢

标签: python list


【解决方案1】:

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

len[i] = input()

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

解决方案:将您的 len 命名为其他名称。

【讨论】:

    【解决方案2】:

    当您定义一个也是内置函数名称的变量时会发生这种情况。
    将变量 len 更改为其他值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-02-23
      • 2012-12-06
      • 2020-03-08
      • 1970-01-01
      • 2017-11-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多