错误:'列出' object在map()函数中不可调用

时间:2018-04-24 09:59:55

标签: python map-function

def powerof(num):
    return num**2

number = [1,2,3,4,5,6,7,8]
s = list(map( powerof , number))
print(s)

错误:' list'对象不可调用

3 个答案:

答案 0 :(得分:1)

您已在代码中将list定义为变量。

不要这样做。调用变量lst或更具描述性的内容。

复制错误的最小示例:

list = [1, 2, 3]

def powerof(num):
    return num**2

number = [1,2,3,4,5,6,7,8]
s = list(map( powerof , number))
print(s)

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-49-7efe90f8f07a> in <module>()
      5 
      6 number = [1,2,3,4,5,6,7,8]
----> 7 s = list(map( powerof , number))
      8 print(s)

TypeError: 'list' object is not callable

答案 1 :(得分:0)

发生此错误是因为您以前使用过 list 对象。

如果您曾经使用过列表,则永远不要调用list()对象。

    list = [1, 2, 3]    # remove this list, then it will work.

    def powerof(num):
        return num**2

    number = [1,2,3,4,5,6,7,8]

    s = list(map( powerof , number))
    print(s)

输出:-[1, 4, 9, 16, 25, 36, 49, 64]

答案 2 :(得分:-1)

list = [1, 2, 3]    # remove this list, then it will work.

def powerof(num):
    return num**2

number = [1,2,3,4,5,6,7,8]

s = set(map( powerof , number))
print(s)

代替列表使用集