python assign values to list elements in loop

后端 未结 4 2148
醉酒成梦
醉酒成梦 2020-11-27 06:27

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 pyth

4条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-27 06:57

    bar = [0 for x in bar]
    

    Long answer : foo is just a local name, rebinding does not impact the list. Python variables are really just key:value pairs, not symbolic names for memory locations.

提交回复
热议问题