To achieve the result you expected, bind the value of x in the loop to a parameter of the lambda and then reference that parameter, as shown below:def main(): d = {} for x in [1,2]: d[x] = lambda x=x: print(x) d[1]() d[2]()if __name__ == '__main__': main()>>>12 这篇关于Python lambda 不记得 for 循环中的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!