Python: Delete all list indices meeting a certain condition

一笑奈何 提交于 2019-12-07 07:06:59

问题


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.


回答1:


If the list is not large, then the easiest way is to create a new list:

In [7]: old_map = [[-1, 2], [5, -3], [2, 3], [1, -1], [7, 1]]

In [8]: new_map=[[x,y] for x,y in a_map if not (x<0 or y<0)]

In [9]: new_map
Out[9]: [[2, 3], [7, 1]]

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:

the_map = [[-1, 2], [5, -3], [2, 3], [1, -1], [7, 1]]
for i in range(len(the_map)-1,-1,-1):
    pair=the_map[i]
    for coord in pair:
        if coord < 0:
            del the_map[i]

print(the_map)

yields

[[2, 3], [7, 1]]

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.




回答2:


You can use list comprehension for this:

>>> mymap = [[-1, 2], [5, -3], [2, 3], [1, -1], [7, 1]]
>>> mymap = [m for m in mymap if m[0] > 0 and m[1] > 0]
>>> mymap
[[2, 3], [7, 1]]



回答3:


If you do not have any other references to the map list, a list comprehension works best:

map = [[a,b] for (a,b) in map if a > 0 and b > 0]

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 of map:

for coord in map[:]:
    if coord[0] < 0 or coord[1] < 0:
        map.remove(coord)



回答4:


Personally, I prefer in-place modification:

li = [[-1, 2], [5, -3], [2, 3], [1, -1], [7, 1]]
print li,'\n'


N = len(li)
for i,(a,b) in enumerate(li[::-1], start=1):
    if a<0 or b<0:
        del li[N-i]
print li

->

[[-1, 2], [5, -3], [2, 3], [1, -1], [7, 1]] 

[[2, 3], [7, 1]]



回答5:


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.

for index in range(len(map)-1,-1,-1):
    if hasNegativeCoord(map[index]):
        del(map[index])

Not very Pythonic, I admit.




回答6:


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:

def filter_in_place(func, target, invert=False):
    "remove all elements of target where func(elem) is false"
    pos = len(target)-1
    while pos >= 0:
        if (not func(target[pos])) ^ invert:
            del target[pos]
        pos -= 1

In your example, this could be applied as follows:

 >>> data = [[-1, 2], [5, -3], [2, 3], [1, -1], [7, 1]]
 >>> def is_good(elem):
         return elem[0] >= 0 and elem[1] >= 0
 >>> filter_in_place(is_good, data)
 >>> data
 [[2, 3], [7, 1]]

(This is just a list-oriented version of filter_in_place, one which supports all base Python datatypes is a bit more complex).




回答7:


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:

import itertools

l = [(4,-5), (-8,2), (-2,-3), (4,7)]

# Option 1: create a new filtered list
l_filtered = list( itertools.ifilter(lambda p: p[0]>0 and p[1]>0, l) )

# Option 2:
for p in itertools.ifilter(lambda p: p[0]>0 and p[1]>0, l):
    ... <subsequent code on your filtered list> 



回答8:


You probably want del pair instead.



来源:https://stackoverflow.com/questions/7163406/python-delete-all-list-indices-meeting-a-certain-condition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!