51Testing软件测试论坛
标题:
python 中,将lambda函数放进列表,指定访问和循环访问不一样
[打印本页]
作者:
测试积点老人
时间:
2020-9-17 09:59
标题:
python 中,将lambda函数放进列表,指定访问和循环访问不一样
我在python 中,将lambda函数放进列表,指定访问和循环访问不一样,求教各位大佬这么为什么,我如果想直接访问,该怎么处理。(指定都是输出1, 循环却正常输出)
代码:
cons = []
for i in range(5):
temp = {'fun': lambda x: x - i}
cons.append(temp)
print(i, temp['fun'](5), cons[i]['fun'](5))
#print(cons)
print('+' * 30)
print(cons[0]['fun'](5))
print(cons[1]['fun'](5))
print(cons[2]['fun'](5))
print(cons[3]['fun'](5))
print(cons[4]['fun'](5))
print('+' * 30)
for i in range(5):
print(cons[i]['fun'](5))
输出:
0 5 5
1 4 4
2 3 3
3 2 2
4 1 1
++++++++++++++++++++++++++++++
1
1
1
1
1
++++++++++++++++++++++++++++++
5
4
3
2
1
复制代码
作者:
海海豚
时间:
2020-9-18 09:43
https://ask.csdn.net/questions/1063329?sort=votes_count
看下这个
作者:
郭小贱
时间:
2020-9-18 10:17
参考1楼的链接即可~
作者:
bellas
时间:
2020-9-18 10:21
参考下这个链接
https://ask.csdn.net/questions/1063329?sort=votes_count
作者:
qqq911
时间:
2020-9-18 10:36
https://ask.csdn.net/questions/1063329?sort=votes_count
看下这个
作者:
jingzizx
时间:
2020-9-18 13:20
参考楼上链接吧
欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/)
Powered by Discuz! X3.2