简体   繁体   English

删除列表中的元素(Python)

[英]removing elements in list(Python)

I have tried execute this, but it dosn't work properly.我试过执行这个,但它不能正常工作。 My goal was to remove all numbers divided by 2. Could someone advise what is wrong.我的目标是删除除以 2 的所有数字。有人能告诉我什么是错误的。 I really do not understand why '4', '8' are still there.我真的不明白为什么“4”、“8”还在那里。

list = [2,4,9,0,4,6,8,3,43,44]
for e in list:
    if e%2==0:
        list.remove(e)
        print(list)

You can use a list comprehension to generate a new list with only elements you want to keep.您可以使用列表推导生成仅包含您想要保留的元素的新列表。

newList = [x for x in oldList if not isEven(x)]

where the function isEven does something like:函数isEven执行以下操作:

def isEven(target):
    return target % 2 == 0

By the way, your question is a duplicate of the following How to remove items from a list while iterating?顺便说一句,您的问题是以下如何在迭代时从列表中删除项目的重复?

You can try to use list.pop() with the position of the element you want to remove.您可以尝试将 list.pop() 与要删除的元素的位置一起使用。 The '2' and '4' are still there because they are skipped when you remove the number before them (When you remove the '2', the '4' is moved to the previous position) '2' 和 '4' 仍然存在,因为当您删除它们之前的数字时它们会被跳过(当您删除 '2' 时,'4' 被移动到之前的位置)

Try this:尝试这个:

l = [2, 3, 4, 5, 9, 10,30,45]
new=[el for el in l if el % 2]
print(new)

Actually, when you remove an element from the list, the indexing gets changed.实际上,当您从列表中删除一个元素时,索引会发生变化。 So, you can do this list comprehension.所以,你可以做这个列表理解。 Also you can use:你也可以使用:

l = [2, 3, 4, 5, 9, 10,30,45]
new=[filter(lambda x: x % 2, l)]
print(new)

If you want to keep the list instead of creating a new one ( the answer by Thomas Milox is a good one otherwise ), you should iterate backward through the list by index.如果您想保留列表而不是创建一个新列表( Thomas Milox 的答案是一个很好的答案),您应该按索引向后遍历列表。 When you remove an element from a list while iterating forwards through the list you may jump over some elements, not processing them.当您在向前迭代列表时从列表中删除元素时,您可能会跳过某些元素,而不是处理它们。 Going backward ensures that no list element removals move any elements that you may still want to process.后退确保没有列表元素删除移动您可能仍要处理的任何元素。

Here is an example of how this may look for your code:这是一个如何查找您的代码的示例:

list = [2, 4, 9, 0, 4, 6, 8, 3, 43, 44]
for i in range(len(list) - 1, -1, -1):  # start at the last element, go until the first one (index 0 - the last value in the range method will not be reached), go backwards
    if list[i] % 2 == 0:
        del list[i]

You can read a bit more about removing an element by index instead of by value here. 您可以在此处阅读有关按索引而不是按值删除元素的更多信息。 This is required since you would otherwise mutate the list on the wrong position for duplicate values.这是必需的,因为否则您会在错误的位置改变列表以获取重复值。 It may also be a bit faster, since remove needs to iterate through the list, searching for the element to remove while del list[i] may look up the element that needs to be removed by index.它也可能更快一点,因为remove需要遍历列表,搜索要删除的元素而del list[i]可能会通过索引查找需要删除的元素。

Iterating backward through a list is also covered here.此处还介绍了通过列表向后迭代。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM