Python(3.8):映射(lambda x:x.method(),列出_对象)不工作

Python(3.8):映射(lambda x:x.method(),列出_对象)不工作,python,class,dictionary,lambda,python-3.8,Python,Class,Dictionary,Lambda,Python 3.8,当我将map lambda应用于对象列表时,我找不到它不起作用的原因,例如: class test(): def __init__(self, input): self.input =input def print_intput(self): print(self.input) objectlist=[] for count in list(range(1,6)): objectlist.append(test(count)) fo

当我将map lambda应用于对象列表时,我找不到它不起作用的原因,例如:

class test():
    def __init__(self, input):
        self.input =input

    def print_intput(self):
        print(self.input)

objectlist=[]
for count in list(range(1,6)):
    objectlist.append(test(count))


for ob in objectlist:
    ob.print_intput()
"""
This ouputs:
1
2
3
4
5

"""
map(lambda x: x.print_intput(), objectlist)
"""
This has no output
"""

为什么第一个输出方法有效,对于循环很简单,而map(lambda方法在这种情况下不起作用)。

map
返回一个迭代器,当您对它进行迭代时,将对列表中的每个元素一次调用一个lambda并返回结果

将它传递到
列表
将使用迭代器并为您提供输出

list(map(lambda x: x.print_intput(), objectlist))

map
返回一个迭代器,当您对其进行迭代时,将对列表中的每个元素一次调用一个lambda并返回结果

将它传递到
列表
将使用迭代器并为您提供输出

list(map(lambda x: x.print_intput(), objectlist))

(由于OP为列表中的i(范围(…)编写了冗余模式
):
,这与此答案直接相关,您也可以扩展此答案来讨论这个问题-您说什么?)为了将来参考,应该注意,这里生成的列表将
[None,None,None,None,None]
作为
print\u input()
不返回任何内容。Iain Shelvington提出的解决方案只返回任何内容,这显然不是目标。但是,在另一个示例中添加“list”时,如果使用函数而不是类,则不起作用。我找到的唯一解决方案就是使用for循环。我仍然不知道为什么map lambda不起作用。@Karl Knechtel我不明白为什么我的问题被关闭了,我浏览了其他示例,使其成为列表并不是其他示例中建议的解决方案。(因为OP为列表中的I(范围(…)编写了冗余模式
):
这与这个答案直接相关,你可以扩展这个答案来讨论这个问题-你说什么?)为了将来参考,应该注意,这里生成的列表将是
[None,None,None,None,None]
作为
打印输入()
不返回任何内容。Iain Shelvington提出的解决方案只返回任何内容,这显然不是目标。但是,在另一个示例中添加“list”时,如果使用函数而不是类,则不起作用。我找到的唯一解决方案就是使用for循环。我仍然不知道为什么map lambda不起作用,我想。@Karl Knechtel我不明白为什么我的问题被关闭了,我浏览了其他的例子,把它列出来并不是其他例子中提出的解决方案。