Python: Delete all list indices meeting a certain

2019-05-06 17:42发布

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.

8条回答
倾城 Initia
2楼-- · 2019-05-06 18:25

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]]
查看更多
小情绪 Triste *
3楼-- · 2019-05-06 18:25

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).

查看更多
Luminary・发光体
4楼-- · 2019-05-06 18:26

You probably want del pair instead.

查看更多
Evening l夕情丶
5楼-- · 2019-05-06 18:36

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楼-- · 2019-05-06 18:38

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)
查看更多
一纸荒年 Trace。
7楼-- · 2019-05-06 18:39

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> 
查看更多
登录 后发表回答