python - 函数不会返回值
问题描述
我有一个任务来编写我自己的 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],但是当我运行它时,它什么也不返回。
解决方案
您需要添加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]
推荐阅读
- python - 来自网络摄像机的 opencv 流
- bash - 带有 bash 脚本的 Git 镜像卡在 3 for 循环中
- java - AppEngine 上的 HikariCP
- vue.js - 使用 vue + firestore 进行全文搜索的最佳方法
- ibm-cloud - ibmcloud:CIS:TTL值是“自动”是什么意思?
- sql-server - 批量插入不插入数据
- pandas - 如何根据熊猫中的条件划分值并放在下一列中
- arrays - 在自定义类中使用自定义类数组的 VBA Excel 问题
- ionic-framework - 无法使用 pdf js 在移动设备上查看 PDF
- sql - Hive:子查询中的小于运算符错误