参见代码:
mult = [lambda x, i=i:x*i for i in range(4)]
for v in mult:
print(v)
但我的回报价值是:
<function <listcomp>.<lambda> at 0x7fd8b26b9d08>
<function <listcomp>.<lambda> at 0x7fd8b26b9d90>
<function <listcomp>.<lambda> at 0x7fd8b26b9ae8>
<function <listcomp>.<lambda> at 0x7fd8b26b9a60>
我能获得真正的价值吗?
我真正的问题是这行代码:
text = [lambda tw: tw['text'] for x in tweets_data]
我的回报是例如:
<function <listcomp>.<lambda> at 0x7fd8b261abf8>
答案 0 :(得分:2)
你只需用不同的i
创建4个函数a = [lambda x, i=i:x*i for i in range(4)]
for func in a:
print func(1)
0
1
2
3