取下列表中的元素,我有以下列表:根据具体内容
['10 .10.10.10' ,'20 .20.20.20' ,'30 .30.30.30' ,'10 .20.30.40 | locationInherited =真”, '40.40.40.40','8.8.8.8 | locationInherited = true']
我需要删除包含'| locationInherited = true'的所有元素,所以'10.20.30.40 | locationInherited = true'&'8.8。 8.8 | locationInherited = true'需要被移除,因此所有留在列表中的是['10.10.10.10','20.20.20.20','30.30.30','40.40.40.40']
我有试过
for elem in list:
if '|locationInherited=true' in elem:
list.remove(elem)
而且
while list.count('|locationInherited=true') > 0:
list.remove('|locationInherited=true')
既不产生一个错误,但既不取出所有必要的元件。
的[从列表中删除项目,而迭代]可能的复制(https://stackoverflow.com/questions/1207406/remove-items-from-a-list-while-iterating) –
列表用'if'| locationInherited = true'not in elem'来理解元素被复制到新列表中的情况可能是要走的路 –
您不应该在迭代列表时从列表中移除它。你可以建立一个新的列表,但是这是列表解析的部分:'l = [e如果'| locationInherited = true'不在e中] –