Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
嵌套python for循环的变量范围_Python_Variables_For Loop_Scope_Nested - Fatal编程技术网

嵌套python for循环的变量范围

嵌套python for循环的变量范围,python,variables,for-loop,scope,nested,Python,Variables,For Loop,Scope,Nested,我有两个嵌套的for循环和两个列表,我希望其中一个列表在内部循环的一次迭代后重新初始化 def test(): i = ['1','5','9','3','6','4'] for x in xrange(0,len(i)): j = ['6', '7', '9', '3'] newi = i for y in xrange(0,len(j)): newi[x] = j[y] print "i", i print "end of on

我有两个嵌套的for循环和两个列表,我希望其中一个列表在内部循环的一次迭代后重新初始化

def test():
i = ['1','5','9','3','6','4']
for x in xrange(0,len(i)):
    j = ['6', '7', '9', '3']
    newi = i
    for y in xrange(0,len(j)):
        newi[x] = j[y]
        print "i", i
    print "end of one iteration on finner loop"
    print "newi", newi
test()
这是一个伪代码,我希望
newi
的一个干净的新实例是
I
的实例,经过一次内循环迭代后,当前它保留了内循环的值

电流输出:

i ['6', '5', '9', '3', '6', '4']
i ['7', '5', '9', '3', '6', '4']
i ['9', '5', '9', '3', '6', '4']
i ['3', '5', '9', '3', '6', '4']
end of one iteration on inner loop
newi ['3', '5', '9', '3', '6', '4']
i ['3', '6', '9', '3', '6', '4']
i ['3', '7', '9', '3', '6', '4']
i ['3', '9', '9', '3', '6', '4']
i ['3', '3', '9', '3', '6', '4']
end of one iteration on inner loop
newi ['3', '3', '9', '3', '6', '4']
i ['3', '3', '6', '3', '6', '4']
i ['3', '3', '7', '3', '6', '4']
i ['3', '3', '9', '3', '6', '4']
i ['3', '3', '3', '3', '6', '4']
end of one iteration on inner loop
newi ['3', '3', '3', '3', '6', '4']
i ['3', '3', '3', '6', '6', '4']
i ['3', '3', '3', '7', '6', '4']
i ['3', '3', '3', '9', '6', '4']
i ['3', '3', '3', '3', '6', '4']
end of one iteration on inner loop
newi ['3', '3', '3', '3', '6', '4']
i ['3', '3', '3', '3', '6', '4']
i ['3', '3', '3', '3', '7', '4']
i ['3', '3', '3', '3', '9', '4']
i ['3', '3', '3', '3', '3', '4']
end of one iteration on inner loop
newi ['3', '3', '3', '3', '3', '4']
i ['3', '3', '3', '3', '3', '6']
i ['3', '3', '3', '3', '3', '7']
i ['3', '3', '3', '3', '3', '9']
i ['3', '3', '3', '3', '3', '3']
end of one iteration on inner loop
newi ['3', '3', '3', '3', '3', '3']
而不是:

newi = i
替换为:

newi = list(i) # list(i) creates another copy  i