函数不会 return 值

Function won't return value

我有一项任务是编写我自己的地图函数,但我不确定为什么它没有 returning 任何值。下面是代码:

def mymap(func, lst):
    new_lst = []
    for items in lst:
        new_lst.append(func(items))
    return new_lst

mymap(abs, [3,-1, 4, -1, 5, -9])

它应该 return [3, 1, 4, 1, 5, 9],但是当我 运行 它时,它没有 return 任何东西。

您需要在

中添加print
def mymap(func, lst):
    new_lst = []
    for items in lst:
        new_lst.append(func(items))
    return new_lst

print(mymap(abs, [3,-1, 4, -1, 5, -9]))

输出:

[3, 1, 4, 1, 5, 9]