gpt4 book ai didi

Python - list.remove 在列表上迭代时

转载 作者:行者123 更新时间:2023-11-28 21:15:52 26 4
gpt4 key购买 nike

<分区>

在 python 中,我遇到了 list.remove 方法的问题。它并没有删除逻辑上必须存在的内容,并且在列表中留下了大量内容。

import os

def removeAll(content):
for afile in content:
content.remove(afile)
return content
content = removeAll(os.listdir("C:\\Windows\\System32"))
print(content)

但是如果我要在没有循环的情况下删除单个内容

content.remove(content[123]) #If this string hasn't already been removed.

它会起作用的。这是为什么?处理庞大列表的理想替代方案是什么?

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com