用python创建地图

2024-10-01 07:38:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我不明白这个错误是什么,怎么纠正。我想创建一个与此类似的函数:

map(lambda x: x**2, range(5))

我的地图功能:

def map(fct, *liste):
  if len(liste) > 0: 
    for i in liste:
        yield (fct(i))

如果函数调用为:

map(lambda x: x**2, [])

错误消息:

TypeError: unsupported operand type(s) for ** or pow(): 'list' and 'int'

Tags: lambda函数inmapforlenifdef
1条回答
网友
1楼 · 发布于 2024-10-01 07:38:44

删除liste之前的星号:

def map(fct, liste):

不传递空数组,而是不传递任何内容:

map(lambda x: x**2)

而且,这个条件看起来没有必要:if len(liste) > 0:。在这个条件下,如果列表为空,map将返回None,而不是空的生成器。你知道吗

相关问题 更多 >