这里是为了说明什么,我试图做一个例子:Python - 什么是更新列表中元素的简单方法?
class bar:
pass
list1 = []
for i in range(10):
bar1 = bar()
bar1.foo = 0
list1.append(bar1)
# update list1
for i in range(10):
print(list1[i].foo)
我想要的代码的输出上面是
10
10
10
10
10
10
10
10
10
10
在# update list1
一部分,如果我在做这个C#中,我可以做
list1.ForEach(x=>x.foo = 10)
而且我知道在Python我可以做
for x in list1:
x.foo = 10
但是,我怎样才能在Python中以类似于C#中的方式做到这一点?
当然''in in'是最简单的方法。你为什么想要使用lambda? – Blorgbeard
for-loop有什么问题?这基本上是一样的。 – Carcigenicate
Python故意不提供这些工具。使用'for'循环。 – user2357112