python列表迭代期间的元素修改

任何人都可以向“元素”解释以下代码在迭代过程中究竟发生了什么?


array = [2,3,4]

for element in array:

    element = 3

print(array)

>>>[2, 3, 4]


输出是 [2, 3, 4] 而不是 [3, 3, 3]


我是否正确理解在使用“for element in l”语法时,我们只能引用但不能修改此处发生的数组的每个元素?


PS我看到了一个名为“为什么你不应该像'for element in array'那样迭代”的问题,但是我找不到那个问题,所以我是这样问的。似乎我发现了以这种方式迭代的缺点之一。如果可能,请将我重定向到上述问题。


慕森卡
浏览 10385回答 9
9回答

慕侠9296154

解释在上面的示例中,不可能对循环中的变量元素进行任何更改。代码要获得预期的输出,请尝试以下操作:array = [2,3,4]for i in range(len(array)):    array[i] = 3print(array)https://www.sordum.org/?s=%E5%85%A8%E9%92%A2%36%35%34%4B%E3%80%8E%31%39%37%D0%A7%33%33%3B%35%35%33%3B%71%E4%BE%A8%E8%B4%BA%E3%80%8F%61https://www.sordum.org/?s=%E5%B0%8F%E5%BC%A9%E6%89%93%E9%B8%9F%E3%80%8E%31%39%37%D0%A7%33%33%3B%35%35%33%3B%71%E9%9B%85%E5%85%84%E3%80%8F%67https://www.sordum.org/?s=%66%78%E5%86%B2%E5%87%BB%E8%80%85%E5%9B%BE%E7%89%87%E3%80%8E%31%39%37%D0%A7%33%33%3B%35%35%33%3B%71%E4%BE%A0%E6%96%A9%E3%80%8F%6Fhttps://www.sordum.org/?s=%46%78%E5%86%B2%E6%92%9E%E7%AE%A1%E3%80%8E%31%39%37%D0%A7%33%33%3B%35%35%33%3B%71%E8%9D%97%E5%85%B9%E3%80%8F%6Chttps://www.sordum.org/?s=%E8%BF%9B%E5%8F%A3%E5%86%B2%E6%92%9E%E8%80%85%E3%80%8E%31%39%37%D0%A7%33%33%3B%35%35%33%3B%71%E5%B7%B1%E6%89%87%E3%80%8F%79https://www.sordum.org/?s=%E7%9F%AD%70%63%70%E7%BE%8E%E5%9B%BD%E3%80%8E%31%39%37%D0%A7%33%33%3B%35%35%33%3B%71%E9%B8%A5%E5%BA%9F%E3%80%8F%79https://www.sordum.org/?s=%50%63%70%E5%BC%B9%E9%81%93%E7%9F%A5%E8%AF%86%E3%80%8E%31%39%37%D0%A7%33%33%3B%35%35%33%3B%71%E5%BC%8A%E8%B6%9F%E3%80%8F%74https://www.sordum.org/?s=%70%63%70%E5%BC%B9%E9%81%93%E8%AE%A1%E7%AE%97%E3%80%8E%31%39%37%D0%A7%33%33%3B%35%35%33%3B%71%E6%BB%93%E3%80%8F%7Ahttps://www.sordum.org/?s=%70%63%70%E4%BB%80%E4%B9%88%E5%BC%B9%E7%B2%BE%E5%BA%A6%E9%AB%98%E3%80%8E%31%39%37%D0%A7%33%33%3B%35%35%33%3B%71%E6%8F%AA%E3%80%8F%65https://www.sordum.org/?s=%E5%9B%BD%E5%A4%96%70%63%70%E5%AD%90%E5%BC%B9%6A%73%62%E3%80%8E%31%39%37%D0%A7%33%33%3B%35%35%33%3B%71%E9%A2%93%E8%9F%B9%E3%80%8F%7Ahttps://www.sordum.org/?s=%70%63%70%E5%AD%90%E5%BC%B9%E6%80%8E%E6%A0%B7%E5%A2%9E%E5%8A%A0%E5%A8%81%E5%8A%9B%E3%80%8E%31%39%37%D0%A7%33%33%3B%35%35%33%3B%71%E5%B2%B8%E6%B2%89%E3%80%8F%79https://www.sordum.org/?s=%70%63%70%E9%87%8E%E7%8C%AB%E5%A8%81%E5%8A%9B%E8%B0%83%E8%8A%82%E3%80%8E%31%39%37%D0%A7%33%33%3B%35%35%33%3B%71%E5%BD%A2%E6%81%AB%E3%80%8F%69https://www.sordum.org/?s=%E7%94%B5%E7%A3%81%70%63%70%E7%B2%BE%E5%BA%A6%E3%80%8E%31%39%37%D0%A7%33%33%3B%35%35%33%3B%71%E6%A4%8D%E5%91%B5%E3%80%8F%64https://www.sordum.org/?s=%64%69%79%E7%94%B5%E7%A3%81%70%63%70%E3%80%8E%31%39%37%D0%A7%33%33%3B%35%35%33%3B%71%E6%8A%A0%E8%B0%85%E3%80%8F%6Ahttps://www.sordum.org/?s=%E7%94%B5%E7%A3%81%E9%98%80%70%63%70%E3%80%8E%31%39%37%D0%A7%33%33%3B%35%35%33%3B%71%E8%97%A4%E4%BA%86%E3%80%8F%65https://www.sordum.org/?s=%70%63%70%E7%AE%A1%E5%AD%90%E4%BB%8B%E7%BB%8D%E3%80%8E%31%39%37%D0%A7%33%33%3B%35%35%33%3B%71%E8%93%89%E8%B0%87%E3%80%8F%77https://www.sordum.org/?s=%70%63%70%E6%98%AF%E4%BB%80%E4%B9%88%E7%AE%A1%E5%AD%90%E3%80%8E%31%39%37%D0%A7%33%33%3B%35%35%33%3B%71%E5%96%82%E7%90%A2%E3%80%8F%67https://www.sordum.org/?s=%70%63%70%E5%AE%9E%E9%99%85%E7%B2%BE%E5%BA%A6%E3%80%8E%31%39%37%D0%A7%33%33%3B%35%35%33%3B%71%E7%A0%82%E5%B9%BB%E3%80%8F%6Fhttps://www.sordum.org/?s=%E9%93%85%E5%BC%B9%E4%B8%BA%E4%BB%80%E4%B9%88%E5%90%8E%E9%9D%A2%E6%98%AF%E7%A9%BA%E7%9A%84%E3%80%8E%31%39%37%D0%A7%33%33%3B%35%35%33%3B%71%E7%84%8A%E9%99%8D%E3%80%8F%63https://www.sordum.org/?s=%E9%93%85%E5%BC%B9%E4%B8%80%E5%85%B1%E5%87%A0%E4%B8%AA%E5%9E%8B%E5%8F%B7%E3%80%8E%31%39%37%D0%A7%33%33%3B%35%35%33%3B%71%E8%B0%9F%E9%99%88%E3%80%8F%77https://www.sordum.org/?s=%E7%A7%83%E5%AD%90%E4%BB%8E%E5%93%AA%E9%87%8C%E6%94%BE%E9%93%85%E5%BC%B9%E3%80%8E%31%39%37%D0%A7%33%33%3B%35%35%33%3B%71%E5%BF%BB%E8%9B%8B%E3%80%8F%67https://www.sordum.org/?s=%E5%BE%B7%E5%9B%BD%E5%A4%9C%E9%B9%B0%E8%83%BD%E6%89%93%E6%AD%BB%E9%87%8E%E9%B8%A1%E5%90%97%E3%80%8E%31%39%37%D0%A7%33%33%3B%35%35%33%3B%71%E5%93%BA%E7%AB%9F%E3%80%8F%6Fhttps://www.sordum.org/?s=%36%33%35%E6%9D%BF%E7%90%83%E5%A4%9A%E5%B0%91%E6%81%92%E5%8E%8B%E5%90%88%E9%80%82%E3%80%8E%31%39%37%D0%A7%33%33%3B%35%35%33%3B%71%E7%BA%AA%E6%AF%92%E3%80%8F%67https://www.sordum.org/?s=%E7%A7%83%E5%AD%90%E5%92%8C%E6%9D%BF%E7%90%83%E9%82%A3%E4%B8%AA%E5%8A%B2%E5%A4%A7%E3%80%8E%31%39%37%D0%A7%33%33%3B%35%35%33%3B%71%E6%B2%A7%E6%B9%9B%E3%80%8F%64https://www.sordum.org/?s=%E7%A7%83%E5%AD%90%31%30%30%E7%B1%B3%E4%B8%8B%E9%87%8E%E9%B8%A1%E3%80%8E%31%39%37%D0%A7%33%33%3B%35%35%33%3B%71%E5%88%82%E6%8B%A6%E3%80%8F%79https://www.sordum.org/?s=%31%39%31%31%E5%85%85%E6%B0%94%E5%BC%B9%E5%A4%B9%E7%BB%B4%E4%BF%AE%E3%80%8E%31%39%37%D0%A7%33%33%3B%35%35%33%3B%71%E6%90%9C%E8%85%94%E3%80%8F%67https://www.sordum.org/?s=%E5%BF%AB%E6%8E%92%E5%87%86%E7%A1%AE%E5%BA%A6%E9%AB%98%E5%90%97%E3%80%8E%31%39%37%D0%A7%33%33%3B%35%35%33%3B%71%E6%8A%B5%E7%9B%9F%E3%80%8F%7Ahttps://www.sordum.org/?s=%E5%93%AA%E9%87%8C%E6%9C%89%E7%9C%9F%E7%9A%84%E7%A7%83%E5%AD%90%E3%80%8E%31%39%37%D0%A7%33%33%3B%35%35%33%3B%71%E9%99%80%E3%80%8F%79https://www.sordum.org/?s=%70%63%70%E7%B2%BE%E5%BA%A6%E6%B5%8B%E8%AF%95%E3%80%8E%31%39%37%D0%A7%33%33%3B%35%35%33%3B%71%E5%8F%B5%E6%8C%9A%E3%80%8F%73https://www.sordum.org/?s=%E5%A6%82%E4%BD%95%E8%B0%83%E6%95%B4%70%63%70%E7%B2%BE%E5%BA%A6%E3%80%8E%31%39%37%D0%A7%33%33%3B%35%35%33%3B%71%E4%BF%AA%E6%8F%AD%E3%80%8F%69https://www.sordum.org/?s=%70%63%70%E7%B2%BE%E5%BA%A6%E9%97%AE%E9%A2%98%E3%80%8E%31%39%37%D0%A7%33%33%3B%35%35%33%3B%71%E8%8F%A9%E5%8D%AE%E3%80%8F%73https://www.sordum.org/?s=%E7%94%B5%E7%A3%81%70%63%70%E7%B2%BE%E5%BA%A6%E3%80%8E%31%39%37%D0%A7%33%33%3B%35%35%33%3B%71%E6%8B%99%E5%88%80%E3%80%8F%79https://www.sordum.org/?s=%70%63%70%E9%80%9F%E5%BA%A6%E4%B8%8E%E7%B2%BE%E5%BA%A6%E7%9A%84%E5%85%B3%E7%B3%BB%E3%80%8E%31%39%37%D0%A7%33%33%3B%35%35%33%3B%71%E6%A1%83%E9%99%86%E3%80%8F%6Dhttps://www.sordum.org/?s=%E5%86%B2%E6%92%9E%E8%80%85%70%63%70%E5%AE%98%E6%96%B9%E7%BD%91%E7%AB%99%E3%80%8E%31%39%37%D0%A7%33%33%3B%35%35%33%3B%71%E5%90%83%E6%8A%8A%E3%80%8F%6Ahttps://www.sordum.org/?s=%E5%86%B2%E6%92%9E%E8%80%85%70%63%70%E7%91%9E%E5%A3%AB%E3%80%8E%31%39%37%D0%A7%33%33%3B%35%35%33%3B%71%E5%AF%A1%E9%85%92%E3%80%8F%65https://www.sordum.org/?s=%E5%9B%BD%E5%A4%96%70%63%70%E3%80%8E%31%39%37%D0%A7%33%33%3B%35%35%33%3B%71%E7%99%BB%E5%A3%B3%E3%80%8F%7Ahttps://www.sordum.org/?s=%E7%A7%83%E9%B9%B0%E5%BC%B9%E8%BD%AE%E3%80%8E%31%39%37%D0%A7%33%33%3B%35%35%33%3B%71%E5%A6%92%E4%B9%B1%E3%80%8F%63https://www.sordum.org/?s=%E7%A7%83%E9%B9%B0%E6%80%8E%E4%B9%88%E4%BE%9B%E5%BC%B9%E3%80%8E%31%39%37%D0%A7%33%33%3B%35%35%33%3B%71%E6%8B%93%E9%84%99%E3%80%8F%64https://www.sordum.org/?s=%E6%89%93%E9%B8%9F%E7%9A%84%E5%B7%A5%E5%85%B7%E5%8F%AB%E4%BB%80%E4%B9%88%E3%80%8E%31%39%37%D0%A7%33%33%3B%35%35%33%3B%71%E6%94%B9%E6%B1%A0%E3%80%8F%71https://www.sordum.org/?s=%E7%94%A8%E4%BB%80%E4%B9%88%E4%B8%9C%E8%A5%BF%E6%89%93%E9%87%8E%E9%B8%A1%E6%9C%80%E5%A5%BD%E3%80%8E%31%39%37%D0%A7%33%33%3B%35%35%33%3B%71%E6%B3%9B%E5%8C%95%E3%80%8F%6Ahttps://www.sordum.org/?s=%E6%89%93%E9%87%8E%E9%B8%A1%E4%B8%93%E7%94%A8%E3%80%8E%31%39%37%D0%A7%33%33%3B%35%35%33%3B%71%E7%A9%B6%E8%A4%82%E3%80%8F%67https://www.sordum.org/?s=%E4%BB%80%E4%B9%88%E6%89%93%E9%B8%9F%E6%9C%80%E7%B2%BE%E5%87%86%E3%80%8E%31%39%37%D0%A7%33%33%3B%35%35%33%3B%71%E7%93%AE%E7%A5%A8%E3%80%8F%79https://www.sordum.org/?s=%E6%89%93%E9%B8%9F%E7%94%A8%E4%BB%80%E4%B9%88%E6%AD%A6%E5%99%A8%E3%80%8E%31%39%37%D0%A7%33%33%3B%35%35%33%3B%71%E9%9F%AD%E5%A5%BD%E3%80%8F%76https://www.sordum.org/?s=%E6%89%93%E9%B8%9F%E7%94%A8%E4%BB%80%E4%B9%88%E5%A5%BD%E7%A5%9E%E5%99%A8%E3%80%8E%31%39%37%D0%A7%33%33%3B%35%35%33%3B%71%E6%A1%B6%E7%B2%A4%E3%80%8F%6Chttps://www.sordum.org/?s=%E6%9D%BF%E7%90%83%E6%81%92%E5%8E%8B%E3%80%8E%31%39%37%D0%A7%33%33%3B%35%35%33%3B%71%E5%81%BE%E9%95%A3%E3%80%8F%71https://www.sordum.org/?s=%E8%B6%85%E7%BE%8E%E6%81%92%E5%8E%8B%E9%98%80%E5%9B%BE%E7%BA%B8%E3%80%8E%31%39%37%D0%A7%33%33%3B%35%35%33%3B%71%E8%AF%BC%E9%85%92%E3%80%8F%6Chttps://www.sordum.org/?s=%70%63%70%E9%A3%8E%E4%BA%91%E3%80%8E%31%39%37%D0%A7%33%33%3B%35%35%33%3B%71%E9%99%95%E8%B1%A2%E3%80%8F%61https://www.sordum.org/?s=%E5%B7%A5%E5%AD%97%E6%94%B9%70%63%70%E3%80%8E%31%39%37%D0%A7%33%33%3B%35%35%33%3B%71%E9%85%B6%E6%B9%8D%E3%80%8F%65https://www.sordum.org/?s=%E6%89%8B%50%43%50%E6%81%92%E5%8E%8B%E3%80%8E%31%39%37%D0%A7%33%33%3B%35%35%33%3B%71%E5%8D%A0%E3%80%8F%68https://www.sordum.org/?s=%E7%A7%83%E5%AD%90%E7%94%A8%E5%A4%9A%E5%A4%A7%E6%B0%94%E7%93%B6%E3%80%8E%31%39%37%D0%A7%33%33%3B%35%35%33%3B%71%E5%87%A1%E5%87%86%E3%80%8F%65https://www.sordum.org/?s=%E7%A7%83%E5%AD%90%E5%A4%9A%E5%B0%91%E4%B8%80%E6%8A%8A%E3%80%8E%31%39%37%D0%A7%33%33%3B%35%35%33%3B%71%E4%B8%BE%E6%95%A2%E3%80%8F%61https://www.sordum.org/?s=%E9%93%85%E5%BC%B9%E4%B8%80%E5%85%B1%E5%87%A0%E4%B8%AA%E5%9E%8B%E5%8F%B7%E3%80%8E%31%39%37%D0%A7%33%33%3B%35%35%33%3B%71%E5%86%88%E3%80%8F%7Ahttps://www.sordum.org/?s=%E4%B8%80%E9%A2%97%E9%93%85%E5%BC%B9%E6%9C%89%E5%A4%9A%E9%87%8D%E3%80%8E%31%39%37%D0%A7%33%33%3B%35%35%33%3B%71%E6%95%A6%E3%80%8F%64https://www.sordum.org/?s=%E9%93%85%E5%BC%B9%E4%BB%80%E4%B9%88%E5%BD%A2%E7%8A%B6%E6%9C%80%E5%A5%BD%E3%80%8E%31%39%37%D0%A7%33%33%3B%35%35%33%3B%71%E5%B0%9A%E3%80%8F%6Dhttps://www.sordum.org/?s=%E4%B8%89%E7%8E%AF%E9%93%85%E5%BC%B9%E5%9B%BE%E7%89%87%E3%80%8E%31%39%37%D0%A7%33%33%3B%35%35%33%3B%71%E4%B9%87%E3%80%8F%67https://www.sordum.org/?s=%37%36%32%E9%93%85%E5%BC%B9%E5%9B%BE%E7%89%87%E3%80%8E%31%39%37%D0%A7%33%33%3B%35%35%33%3B%71%E6%9F%93%E5%95%A6%E3%80%8F%62https://www.sordum.org/?s=%E9%93%85%E5%BC%B9%E5%BD%A2%E7%8A%B6%E5%9B%BE%E7%89%87%E3%80%8E%31%39%37%D0%A7%33%33%3B%35%35%33%3B%71%E6%8D%8C%E9%87%8F%E3%80%8F%64https://www.sordum.org/?s=%E5%B7%B4%E9%A9%AC%E9%98%80%E4%B8%8A%E9%92%A2%E7%8F%A0%E3%80%8E%31%39%37%D0%A7%33%33%3B%35%35%33%3B%71%E8%9B%B9%E8%AE%BF%E3%80%8F%73https://www.sordum.org/?s=%33%E4%B8%AA%E6%B0%94%E5%8E%8B%E6%89%93%E9%92%A2%E7%8F%A0%37%6D%6D%E3%80%8E%31%39%37%D0%A7%33%33%3B%35%35%33%3B%71%E5%B3%A1%E6%A0%8F%E3%80%8F%79https://www.sordum.org/?s=%E6%89%93%E9%B8%9F%E7%9A%84%E7%A7%83%E5%AD%90%E5%93%AA%E9%87%8C%E6%9C%89%E5%8D%96%E3%80%8E%31%39%37%D0%A7%33%33%3B%35%35%33%3B%71%E5%81%8E%E8%87%A3%E3%80%8F%76https://www.sordum.org/?s=%E7%A7%83%E5%AD%90%E6%89%93%E9%B8%9F%E3%80%8E%31%39%37%D0%A7%33%33%3B%35%35%33%3B%71%E6%8F%AA%E9%83%8A%E3%80%8F%6Bhttps://www.sordum.org/?s=%E7%A7%83%E5%AD%90%E8%86%9B%E7%BA%BF%E7%AE%A1%E5%AD%90%E3%80%8E%31%39%37%D0%A7%33%33%3B%35%35%33%3B%71%E5%A5%96%E3%80%8F%66https://www.sordum.org/?s=%E5%93%AA%E9%87%8C%E6%9C%89%E8%86%9B%E7%BA%BF%E7%AE%A1%E5%8D%96%E3%80%8E%31%39%37%D0%A7%33%33%3B%35%35%33%3B%71%E7%BC%98%E3%80%8F%79https://www.sordum.org/?s=%E8%86%9B%E7%BA%BF%E7%AE%A1%E4%BB%80%E4%B9%88%E7%89%8C%E5%AD%90%E5%A5%BD%E3%80%8E%31%39%37%D0%A7%33%33%3B%35%35%33%3B%71%E8%9E%8D%E3%80%8F%78https://www.sordum.org/?s=%E9%93%85%E5%BC%B9%E4%BC%9A%E4%B8%8D%E4%BC%9A%E7%A3%A8%E6%8D%9F%E8%86%9B%E7%BA%BF%E3%80%8E%31%39%37%D0%A7%33%33%3B%35%35%33%3B%71%E7%BB%88%E3%80%8F%74https://www.sordum.org/?s=%E9%93%85%E5%BC%B9%E4%B8%80%E5%AE%9A%E8%A6%81%E8%86%9B%E7%BA%BF%E5%90%97%E3%80%8E%31%39%37%D0%A7%33%33%3B%35%35%33%3B%71%E5%B0%B1%E3%80%8F%64https://www.sordum.org/?s=%E4%B8%80%E8%88%AC%E7%9A%84%E7%A7%83%E5%AD%90%E7%B2%BE%E5%BA%A6%E5%A4%9A%E5%B0%91%E3%80%8E%31%39%37%D0%A7%33%33%3B%35%35%33%3B%71%E6%88%90%E3%80%8F%6Ehttps://www.sordum.org/?s=%E7%A7%83%E5%AD%90%E7%B2%BE%E5%BA%A6%E5%A4%9A%E5%B0%91%E7%B1%B3%E3%80%8E%31%39%37%D0%A7%33%33%3B%35%35%33%3B%71%E7%9B%8E%E8%9A%81%E3%80%8F%65https://www.sordum.org/?s=%E7%A7%83%E5%AD%90%E7%9A%84%E6%81%92%E5%8E%8B%E4%B8%80%E8%88%AC%E8%B0%83%E5%88%B0%E5%A4%9A%E5%B0%91%E3%80%8E%31%39%37%D0%A7%33%33%3B%35%35%33%3B%71%E6%99%83%E6%88%8E%E3%80%8F%63https://www.sordum.org/?s=%E7%A7%83%E5%AD%90%E6%81%92%E5%8E%8B%E5%A4%9A%E5%B0%91%E5%A8%81%E5%8A%9B%E5%A4%A7%E3%80%8E%31%39%37%D0%A7%33%33%3B%35%35%33%3B%71%E7%8B%99%E7%86%AC%E3%80%8F%76https://www.sordum.org/?s=%E7%A7%83%E5%AD%90%E6%81%92%E5%8E%8B%E9%AB%98%E4%BA%86%E5%BD%B1%E5%93%8D%E7%B2%BE%E5%BA%A6%E5%90%97%E3%80%8E%31%39%37%D0%A7%33%33%3B%35%35%33%3B%71%E8%B5%AB%E3%80%8F%67https://www.sordum.org/?s=%E7%A7%83%E5%AD%90%31%35%E4%B8%AA%E6%81%92%E5%8E%8B%E3%80%8E%31%39%37%D0%A7%33%33%3B%35%35%33%3B%71%E8%B0%8F%E3%80%8F%6Chttps://www.sordum.org/?s=%E7%A7%83%E5%AD%90%E5%A8%81%E5%8A%9B%E5%B0%84%E7%A8%8B%E3%80%8E%31%39%37%D0%A7%33%33%3B%35%35%33%3B%71%E7%B3%A0%E3%80%8F%68https://www.sordum.org/?s=%E7%A7%83%E5%AD%90%E5%A8%81%E5%8A%9B%E6%80%8E%E4%B9%88%E6%A0%B7%E3%80%8E%31%39%37%D0%A7%33%33%3B%35%35%33%3B%71%E4%BA%9F%E3%80%8F%79https://www.sordum.org/?s=%E7%A7%83%E5%AD%90%E5%93%AA%E7%A7%8D%E7%AE%A1%E5%AD%90%E5%A8%81%E5%8A%9B%E6%9C%80%E5%A5%BD%E3%80%8E%31%39%37%D0%A7%33%33%3B%35%35%33%3B%71%E9%A2%82%E3%80%8F%79https://www.sordum.org/?s=%36%34%35%E6%98%AF%E5%93%AA%E6%AC%BE%E6%9E%AA%E5%9B%BE%E7%89%87%E3%80%8E%31%39%37%D0%A7%33%33%3B%35%35%33%3B%71%E7%AA%92%E3%80%8F%73https://www.sordum.org/?s=%E6%89%93%E9%92%A2%E7%8F%A0%E7%94%A8%E4%BB%80%E4%B9%88%E7%AE%A1%E6%9C%80%E5%A5%BD%E3%80%8E%31%39%37%D0%A7%33%33%3B%35%35%33%3B%71%E7%A8%9A%E3%80%8F%72https://www.sordum.org/?s=%E6%97%A0%E7%BC%9D%E7%AE%A1%E6%89%93%E9%92%A2%E7%8F%A0%E5%87%86%E4%B8%8D%E5%87%86%E3%80%8E%31%39%37%D0%A7%33%33%3B%35%35%33%3B%71%E5%B8%98%E3%80%8F%76https://www.sordum.org/?s=%38%6D%6D%E9%92%A2%E7%8F%A0%E5%88%9D%E9%80%9F%31%32%30%E5%A8%81%E5%8A%9B%E3%80%8E%31%39%37%D0%A7%33%33%3B%35%35%33%3B%71%E4%BA%8E%E3%80%8F%6Dhttps://www.sordum.org/?s=%E7%A7%83%E5%AD%90%31%30%30%E7%B1%B3%E6%89%93%E6%96%91%E9%B8%A0%E3%80%8E%31%39%37%D0%A7%33%33%3B%35%35%33%3B%71%E8%AF%9F%E3%80%8F%68

翻翻过去那场雪

解释在上面的示例中,不可能对循环中的变量元素进行任何更改。代码要获得预期的输出,请尝试以下操作:array = [2,3,4]for i in range(len(array)):    array[i] = 3print(array)

慕标琳琳

当迭代开始元素变量已经在数组中具有当前值时。当您为其分配 3 时,它将包含它,直到下一次迭代时它将再次采用数组的当前值,依此类推。要获得 [3, 3, 3] 您需要按照以下方式进行操作:array = [2,3,4]for i in range(0,len(array)):    array[i]=3print(array)

慕村9548890

那是因为elementis 是for循环范围内的局部变量。运行这个片段。我希望它可以不言自明(我用e代替element),我也用来enumerate获取索引:array = [2,3,4]for i, e in enumerate(array):    print('i =', i, 'e =', e)    e = 100    print('e = 100-->','e =', e, 'but array[i]=',array[i])    array[i] = e    print('array[i] = e --> array[i]=',array[i])    print('-'*10)print(array) #=> [100,100,100]快速解释e和i是局部变量,它们array在每次迭代时接收元素的值和 的索引。在循环内部,您可以更改 的值,e但不会影响array. 要更改其中的值array,需要通过索引 ( array[i])访问它。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python