用python创建地图
我不明白这个错误是什么,以及如何纠正它们。我想创建一个类似于此的函数:用python创建地图,python,python-3.x,Python,Python 3.x,我不明白这个错误是什么,以及如何纠正它们。我想创建一个类似于此的函数: 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 *
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'
在列表之前删除星号:
或者,不传递空数组,而不传递任何内容:
map(lambda x: x**2)
此外,此条件看起来不必要:如果lenliste>0:。在这种情况下,如果列表为空,map将返回None而不是空的生成器 谢谢你的回答。对不起,我的英语,它是我的第二语言。
map(lambda x: x**2)