首页 > 解决方案 > 函数不会返回值

问题描述

我有一个任务来编写我自己的 map 函数,但我不确定它为什么没有返回任何值。下面是代码:

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])

它应该返回 [3, 1, 4, 1, 5, 9],但是当我运行它时,它什么也不返回。

标签: pythonfunctionmapreducereturn

解决方案


您需要添加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]

推荐阅读