简单示例:
myList = [1, 2, 3, 4, 5]
for obj in myList:
obj += 1
print myList
打印
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]
同时:
myList = [1, 2, 3, 4, 5]
for index in range(0,len(myList)):
myList[index] += 1
print myList
打印
[1, 2, 3, 4, 5]
[2, 3, 4, 5, 6]
结论:
我能找到的所有示例代码都使用全局列表访问器来就地修改列表.修改列表迭代器就这么邪恶吗?
All example code I can find uses the global list accessors to modify the list inplace. Is it so evil to modify a list iterator?
在 for obj in myList: 中,在每次迭代中,obj 是myList 中的元素.所以对 obj 的更改对 myList 的元素没有任何影响.
in for obj in myList:, in every iteration, obj is a (shallow) copy of the element in myList. So the change on the obj does nothing to myList's elements.
这与 Perl for my $obj (@myList) {}
这篇关于不允许在 Python 中修改列表迭代器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
如何在python中的感兴趣区域周围绘制一个矩形How to draw a rectangle around a region of interest in python(如何在python中的感兴趣区域周围绘制一个矩形)
如何使用 OpenCV 检测和跟踪人员?How can I detect and track people using OpenCV?(如何使用 OpenCV 检测和跟踪人员?)
如何在图像的多个矩形边界框中应用阈值?How to apply threshold within multiple rectangular bounding boxes in an image?(如何在图像的多个矩形边界框中应用阈值?)
如何下载 Coco Dataset 的特定部分?How can I download a specific part of Coco Dataset?(如何下载 Coco Dataset 的特定部分?)
根据文本方向检测图像方向角度Detect image orientation angle based on text direction(根据文本方向检测图像方向角度)
使用 Opencv 检测图像中矩形的中心和角度Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 检测图像中矩形的中心和角度)