Same variable name for different values in nested loops.

前端 未结 6 1093
囚心锁ツ
囚心锁ツ 2020-12-22 12:26

This code is perfectly valid Python

x=[[1,2,3,4], [11,22,33,44]]
for e in x:
    for e in e:
        print e

Can someone please tell me why

6条回答
  •  遥遥无期
    2020-12-22 12:37

    e is just a label. Each iteration of your outer loop, e is assigned the nth value from x, and each inner loop iteration it is assigned the mth value from x[n]. It's perfectly valid Python code, it is just not advisable from a style perspective because outside of a trivial example, it can quickly become confusing what e represents at what point in the code, and thus is likely to lead to bugs.

提交回复
热议问题