python assign values to list elements in loop
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Is this a valid python behavior? I would think that the end result should be [0,0,0] and the id() function should return identical values each iteration. How to make it pythonic, and not use enumerate or range(len(bar))? bar = [1,2,3] print bar for foo in bar: print id (foo) foo=0 print id(foo) print bar output: [1, 2, 3] 5169664 5169676 5169652 5169676 5169640 5169676 [1, 2, 3] 回答1: First of all, you cannot reassign a loop variable―well, you can, but that won’t change the list you are iterating over. So setting foo = 0 will not change the