Python:删除满足特定条件的所有列表索引
为了直接解决这个问题,我试图迭代 python 中的坐标对列表,并删除其中一个坐标为负数的所有情况。例如:
在数组中:
map = [[-1, 2], [5, -3], [2, 3], [1, -1], [7, 1]]
我想删除其中任一坐标 < 的所有对。 0,离开:
map = [[2, 3], [7, 1]]
我的问题是python列表不能有任何间隙,所以如果我像这样循环:
i = 0
for pair in map:
for coord in pair:
if coord < 0:
del map[i]
i += 1
当元素被删除时,所有索引都会移动,搞乱迭代并导致各种问题。我尝试将坏元素的索引存储在另一个列表中,然后循环并删除这些元素,但我遇到了同样的问题:一旦一个元素消失,整个列表的移位和索引就不再准确。
我有什么遗漏的吗?
谢谢。
to get right down to it, I'm trying to iterate through a list of coordinate pairs in python and delete all cases where one of the coordinates is negative. For example:
in the array:
map = [[-1, 2], [5, -3], [2, 3], [1, -1], [7, 1]]
I want to remove all the pairs in which either coordinate is < 0, leaving:
map = [[2, 3], [7, 1]]
My problem is that python lists cannot have any gaps, so if I loop like this:
i = 0
for pair in map:
for coord in pair:
if coord < 0:
del map[i]
i += 1
All the indices shift when the element is deleted, messing up the iteration and causing all sorts of problems. I've tried storing the indices of the bad elements in another list and then looping through and deleting those elements, but I have the same problem: once one is gone, the whole list shifts and indices are no longer accurate.
Is there something I'm missing?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您可以使用 列表理解 来实现此目的:
You can use list comprehension for this:
如果您没有对
map
列表的任何其他引用,则列表理解效果最好:如果您确实有其他引用并且需要实际从
map 引用的列表中删除元素
,您必须迭代map
的副本:If you do not have any other references to the
map
list, a list comprehension works best:If you do have other references and need to actually remove elements from the list referenced by
map
, you have to iterate over a copy ofmap
:就我个人而言,我更喜欢就地修改:
->
Personally, I prefer in-place modification:
->
如果您希望就地执行此操作,而不创建新列表,只需使用索引从 len(map)-1 到 0 运行的 for 循环即可。
我承认,这不是很 Pythonic。
If you wish to do this in place, without creating a new list, simply use a for loop with index running from len(map)-1 down to 0.
Not very Pythonic, I admit.
如果列表足够小,则制作仅包含所需元素的副本会更有效,如其他答案中详述。
但是,如果列表太大,或者由于某些其他原因您需要从列表对象中删除元素就地,我发现以下小辅助函数非常有用:
在您的示例中,这可以按如下方式应用:(
这只是 filter_in_place 的面向列表的版本,支持所有基本 Python 数据类型的版本稍微复杂一些)。
If the list is small enough, it's more efficient to make a copy containing just the elements you need, as detailed in the other answers.
However, if the list is too large, or for some other reason you need to remove the elements from the list object in place, I've found the following little helper function quite useful:
In your example, this could be applied as follows:
(This is just a list-oriented version of filter_in_place, one which supports all base Python datatypes is a bit more complex).
itertools.ifilter()/ifilterfalse()
存在为此:通过谓词过滤可迭代对象(显然不是就地)。更好的是,如果可能的话,避免创建和分配整个过滤列表对象,只需迭代它:
itertools.ifilter()/ifilterfalse()
exist to do exactly this: filter an iterable by a predicate (not in-place, obviously).Better still, avoid creating and allocating the entire filtered list object if at all possible, just iterate over it:
您可能需要
delpair
。You probably want
del pair
instead.如果列表不大,那么最简单的方法是创建一个新列表:
如果您想丢弃其他对,可以使用
old_map = new_map
进行后续操作。如果列表太大,创建一个大小相当的新列表是一个问题,那么您可以就地从列表中删除元素——诀窍是首先从尾端删除它们:
产生
PS。 map 是一个非常有用的内置 Python 函数。最好不要将变量命名为
map
,因为这会覆盖内置变量。If the list is not large, then the easiest way is to create a new list:
You can follow this up with
old_map = new_map
if you want to discard the other pairs.If the list is so large creating a new list of comparable size is a problem, then you can delete elements from a list in-place -- the trick is to delete them from the tail-end first:
yields
PS. map is such a useful built-in Python function. It is best not to name a variable
map
since this overrides the built-in.