variable doesn't reset in while loop with function
我以前从未遇到过这种情况,我想知道是否有人能找到解决办法。我有一个while循环,其中包含一个返回整数的函数。函数接受一个字符串和一个列表。while循环的第一次迭代返回正确的答案,但在随后的迭代中,列表似乎是空的,即使函数不会以任何方式更改列表。另外,如果我尝试在while循环中的函数之后重置列表,新列表似乎也是空的。这似乎是很奇怪的行为。对于发生的一切,任何解释都将不胜感激。循环中函数的代码很长,因此在这个阶段我将避免发布它。不过,如果有要求,我会发帖。
1 2 3 4 5 6 7 8 9 10 11 12 13 | spectrum = [1,2,3,4,5,6] leaderboard = ['zzz','xxx','yyy'] pep_scores = [] j=0 original_spectrum = spectrum print len(original_spectrum) while j < len(leaderboard): x= linear_score(leaderboard[j],spectrum) #this function doesn't alter spectrum print leaderboard[j], x spectrum = original_spectrum #should reset spectrum even though it shouldn't be necessary to do that print len(spectrum), len(original_spectrum) #prints 2 empty lists pep_scores.append(x) #appends correct score on 1st iteration and '0' for all others j=j+1 |
我添加了print语句来解决这个问题,我的原始代码在while循环中没有包含"original_spectrum=spectrum"或"spectrum=original_spectrum"。我不明白为什么一次迭代后,"原始光谱"是一个空列表。我没有发布函数,因为我看不到它是如何导致问题的。请询问您是否需要更多信息。
要创建列表的副本,请使用
所以在你的例子中:
1 2 3 4 5 6 7 8 9 10 11 12 13 | spectrum = [1,2,3,4,5,6] leaderboard = ['zzz','xxx','yyy'] pep_scores = [] j=0 original_spectrum = spectrum[:] print len(original_spectrum) while j < len(leaderboard): x= linear_score(leaderboard[j],spectrum) #this function doesn't alter spectrum print leaderboard[j], x spectrum = original_spectrum[:] #should reset spectrum even though it shouldn't be necessary to do that print len(spectrum), len(original_spectrum) #prints 2 empty lists pep_scores.append(x) #appends correct score on 1st iteration and '0' for all others j=j+1 |
因为您在函数外部定义了
要更好地理解,请参见以下示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | >>> a=[1,2,3] >>> def f(s): ... s.remove(1) ... >>> f(a) >>> a [2, 3] >>> def f(s): ... s+=[1,2] ... >>> f(a) >>> a [2, 3, 1, 2] |
现在您有两种选择: