复制列表的方法:
lst = [1,2,3]
lst1 = lst[:] # one way
lst2 = list(lst) # another
删除数据的正确方法:
num_list = [1, 2, 3, 4, 5]
print(num_list)
for item in num_list[:]:
if item == 2: num_list.remove(item)
else:
print(item)
print(num_list)
作者:方小圆
链接:https://www.zhihu.com/question/49098374/answer/152953958
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
列表操作技巧
本文介绍了Python中复制列表的有效方法,包括使用切片语法和list()函数。同时,文章提供了删除列表中特定元素的正确示例代码,确保在遍历过程中正确移除元素,避免跳过的情况。
14万+

被折叠的 条评论
为什么被折叠?



