代码之家  ›  专栏  ›  技术社区  ›  Ezra Lazuardy

将映射转换为列表返回TypeError:“int”对象不可调用

  •  2
  • Ezra Lazuardy  · 技术社区  · 6 年前

    我正在尝试将地图转换为列表,但它已返回 TypeError: 'int' object is not callable

    n = int(input())
    arr = list(map(n, input().split()))
    for i in arr:
        print(i)
    

    错误:

    arr = list(map(n, input().split()))
    TypeError: 'int' object is not callable
    
    1 回复  |  直到 6 年前
        1
  •  3
  •   user4548084 user4548084    6 年前

    Python中的map函数接受第一个参数作为可调用函数,第二个参数作为iterable函数。iterable中的每一项都通过函数传递到一个输出中 map

    在你的例子中, n 定义为 int n = int(input()) . 因此,上述代码相当于:

    n = int(input())
    out = []
    
    for element in input().split():
        out.append( n(element) )
    

    此代码无法运行,因为 (作为 内景

    如果你想做的是像 0 2 45 119 对于整数列表,可以传递 地图 作为 ,和 内景 将在提供的输入的每个元素上调用。