我有一个函数,它决定数字是小于0还是根本没有数字
def numberfunction(s) :
if s == "":
return 0
if s < 0 :
return -1
if s > 0:
return s
我还有一张单子
numbers = [[]]
现在,假设我在列表中填充了如下数字:
[[1,2,3,4],[1,1,1,1],[2,2,2,2] ..etc ]
我该如何将上面的函数调用到列表中的数字中呢?你知道吗
我需要在每个列表的每个数字上使用函数的循环,还是比这个简单?你知道吗
Tags:
可以使用^{} 和list comprehension将函数应用于所有元素。请注意,我已经修改了您的示例列表以显示所有退货案例。你知道吗
注意,按照
numberfunction
目前的工作方式,对于等于零的元素,它将返回None
(感谢@thefourtheye指出这一点)。你知道吗你可以这样做:
也可以调用嵌套的
map()
:我有Python<;3,其中map返回list。你知道吗
相关问题 更多 >
编程相关推荐