0
通过有关该问题的帖子去了,但没有帮助我理解这个问题或解决问题:'int'对象不可迭代python3?
# This is the definition of the square() function
def square(lst1):
lst2 = []
for num in lst1:
lst2.append(num**2)
return lst2
n = [4,3,2,1]
print(list(map(square, n)))
>>>
File "test.py", line 5, in square
for num in lst1:
TypeError: 'int' object is not iterable
什么是错的square()
功能定义,行,该如何解决? 非常感谢!
[编辑]你的问题,不要在评论中填写。 –
现在“方形”太复杂了。 'map'一次传递一个整数。你需要'def square(n):return n * n' –
你不需要在你的函数中为'lst1'中的num。该功能一次只收到一个列表元素。只要'返回lst1 ** 2';返回一次性列表通常无用 – WillardSolutions