[英]Elegant way to remove items from sequence in Python? [duplicate]

This question already has an answer here:


When I am writing code in Python, I often need to remove items from a list or other sequence type based on some criteria. I haven't found a solution that is elegant and efficient, as removing items from a list you are currently iterating through is bad. For example, you can't do this:


for name in names:
    if name[-5:] == 'Smith':

I usually end up doing something like this:


toremove = []
for name in names:
    if name[-5:] == 'Smith':
for name in toremove:
del toremove

This is innefficient, fairly ugly and possibly buggy (how does it handle multiple 'John Smith' entries?). Does anyone have a more elegant solution, or at least a more efficient one?

這是一種非常復雜的、相當丑陋的、可能有bug的(它如何處理多個“John Smith”條目?)有人有更優雅的解決方案嗎,或者至少有更高效的方案嗎?

How about one that works with dictionaries?


14 个解决方案



Two easy ways to accomplish just the filtering are:


  1. Using filter:


    names = filter(lambda name: name[-5:] != "Smith", names)

    name = filter(lambda name: name[-5:] != "Smith", names)

  2. Using list comprehensions:


    names = [name for name in names if name[-5:] != "Smith"]

    名稱=[名稱中名稱的名稱,如果名稱為[-5:]!= "史密斯"]

Note that both cases keep the values for which the predicate function evaluates to True, so you have to reverse the logic (i.e. you say "keep the people who do not have the last name Smith" instead of "remove the people who have the last name Smith").


Edit Funny... two people individually posted both of the answers I suggested as I was posting mine.




You can also iterate backwards over the list:


for name in reversed(names):
    if name[-5:] == 'Smith':

This has the advantage that it does not create a new list (like filter or a list comprehension) and uses an iterator instead of a list copy (like [:]).


Note that although removing elements while iterating backwards is safe, inserting them is somewhat trickier.




The obvious answer is the one that John and a couple other people gave, namely:


>>> names = [name for name in names if name[-5:] != "Smith"]       # <-- slower

But that has the disadvantage that it creates a new list object, rather than reusing the original object. I did some profiling and experimentation, and the most efficient method I came up with is:


>>> names[:] = (name for name in names if name[-5:] != "Smith")    # <-- faster

Assigning to "names[:]" basically means "replace the contents of the names list with the following value". It's different from just assigning to names, in that it doesn't create a new list object. The right hand side of the assignment is a generator expression (note the use of parentheses rather than square brackets). This will cause Python to iterate across the list.


Some quick profiling suggests that this is about 30% faster than the list comprehension approach, and about 40% faster than the filter approach.


Caveat: while this solution is faster than the obvious solution, it is more obscure, and relies on more advanced Python techniques. If you do use it, I recommend accompanying it with a comment. It's probably only worth using in cases where you really care about the performance of this particular operation (which is pretty fast no matter what). (In the case where I used this, I was doing A* beam search, and used this to remove search points from the search beam.)

注意:雖然這個解決方案比顯而易見的解決方案要快,但是它更加模糊,並且依賴於更高級的Python技術。如果您確實使用了它,我建議您在它后面加上一條注釋。它可能只在您真正關心這個特定操作的性能的情況下才值得使用(無論如何它都非常快)。(在我使用這個的情況下,我做了一個* beam搜索,並用它從搜索光束中移除搜索點。)



Using a list comprehension


list = [x for x in list if x[-5:] != "smith"]



There are times when filtering (either using filter or a list comprehension) doesn't work. This happens when some other object is holding a reference to the list you're modifying and you need to modify the list in place.


for name in names[:]:
    if name[-5:] == 'Smith':

The only difference from the original code is the use of names[:] instead of names in the for loop. That way the code iterates over a (shallow) copy of the list and the removals work as expected. Since the list copying is shallow, it's fairly quick.




filter would be awesome for this. Simple example:


names = ['mike', 'dave', 'jim']
filter(lambda x: x != 'mike', names)
['dave', 'jim']

Edit: Corey's list comprehension is awesome too.




names = filter(lambda x: x[-5:] != "Smith", names);



Both solutions, filter and comprehension requires building a new list. I don't know enough of the Python internals to be sure, but I think that a more traditional (but less elegant) approach could be more efficient:


names = ['Jones', 'Vai', 'Smith', 'Perez']

item = 0
while item <> len(names):
    name = names [item]
    if name=='Smith':
        item += 1

print names

Anyway, for short lists, I stick with either of the two solutions proposed earlier.




To answer your question about working with dictionaries, you should note that Python 3.0 will include dict comprehensions:

要回答關於使用字典的問題,您應該注意Python 3.0將包括字典的理解:

>>> {i : chr(65+i) for i in range(4)}

In the mean time, you can do a quasi-dict comprehension this way:


>>> dict([(i, chr(65+i)) for i in range(4)])

Or as a more direct answer:


dict([(key, name) for key, name in some_dictionary.iteritems if name[-5:] != 'Smith'])



If the list should be filtered in-place and the list size is quite big, then algorithms mentioned in the previous answers, which are based on list.remove(), may be unsuitable, because their computational complexity is O(n^2). In this case you can use the following no-so pythonic function:

如果列表應該過濾就地和列表大小相當大,那么算法在前面提到的答案,這是基於list.remove(),可能是不適合的,因為他們的計算復雜度是O(n ^ 2)。在這種情況下,您可以使用下面的no-so python函數:

def filter_inplace(func, original_list):
  """ Filters the original_list in-place.

  Removes elements from the original_list for which func() returns False.

  Algrithm's computational complexity is O(N), where N is the size
  of the original_list.

  # Compact the list in-place.
  new_list_size = 0
  for item in original_list:
    if func(item):
      original_list[new_list_size] = item
      new_list_size += 1

  # Remove trailing items from the list.
  tail_size = len(original_list) - new_list_size
  while tail_size:
    tail_size -= 1

a = [1, 2, 3, 4, 5, 6, 7]

# Remove even numbers from a in-place.
filter_inplace(lambda x: x & 1, a)

# Prints [1, 3, 5, 7]
print a

Edit: Actually, the solution at https://stackoverflow.com/a/4639748/274937 is superior to mine solution. It is more pythonic and works faster. So, here is a new filter_inplace() implementation:


def filter_inplace(func, original_list):
  """ Filters the original_list inplace.

  Removes elements from the original_list for which function returns False.

  Algrithm's computational complexity is O(N), where N is the size
  of the original_list.
  original_list[:] = [item for item in original_list if func(item)]



The filter and list comprehensions are ok for your example, but they have a couple of problems:


  • They make a copy of your list and return the new one, and that will be inefficient when the original list is really big
  • 他們復制你的列表並返回新的列表,當原始列表真的很大時,這將是低效的
  • They can be really cumbersome when the criteria to pick items (in your case, if name[-5:] == 'Smith') is more complicated, or has several conditions.
  • 當選擇項(在您的例子中,如果名稱[-5:]= 'Smith')的條件比較復雜或有幾個條件時,它們可能會非常麻煩。

Your original solution is actually more efficient for very big lists, even if we can agree it's uglier. But if you worry that you can have multiple 'John Smith', it can be fixed by deleting based on position and not on value:


names = ['Jones', 'Vai', 'Smith', 'Perez', 'Smith']

toremove = []
for pos, name in enumerate(names):
    if name[-5:] == 'Smith':
for pos in sorted(toremove, reverse=True):

print names

We can't pick a solution without considering the size of the list, but for big lists I would prefer your 2-pass solution instead of the filter or lists comprehensions




In the case of a set.


toRemove = set([])  
for item in mySet:  
    if item is unwelcome:  
mySets = mySet - toRemove 



Here is my filter_inplace implementation that can be used to filter items from a list in-place, I came up with this on my own independently before finding this page. It is the same algorithm as what PabloG posted, just made more generic so you can use it to filter lists in place, it is also able to remove from the list based on the comparisonFunc if reversed is set True; a sort-of of reversed filter if you will.


def filter_inplace(conditionFunc, list, reversed=False):
    index = 0
    while index < len(list):
        item = list[index]

        shouldRemove = not conditionFunc(item)
        if reversed: shouldRemove = not shouldRemove

        if shouldRemove:
            index += 1



Well, this is clearly an issue with the data structure you are using. Use a hashtable for example. Some implementations support multiple entries per key, so one can either pop the newest element off, or remove all of them.


But this is, and what you're going to find the solution is, elegance through a different data structure, not algorithm. Maybe you can do better if it's sorted, or something, but iteration on a list is your only method here.


edit: one does realize he asked for 'efficiency'... all these suggested methods just iterate over the list, which is the same as what he suggested.




粤ICP备14056181号  © 2014-2020 ITdaan.com