How to modify python collections by filtering in-place?

寵の児 提交于 2019-12-18 19:18:43

问题


I was wondering, if there is way in Python to modify collections without creating new ones. E.g.:

lst = [1, 2, 3, 4, 5, 6]
new_lst = [i for i in lst if i > 3]

Works just fine, but a new collection is created. Is there a reason, that Python collections lack a filter() method (or similar) that would modify the collection object in place?


回答1:


The other answers are correct; if you want all the names pointing to the old list to point to the new list you can use slice assignment.

However, that's not truly in-place creation; the new list is first created elsewhere. The link in Sven's answer is good.

The reason there isn't one that truly operates in-place is that while making a new list like that is O(n), each truly in-place item removal would be O(k) by itself, where k is the length of the list from the removal point on. The only way to avoid that with Python lists is to use some temporary storage, which is what you're doing by using slice assignment.

An example of an in-place O(n) filter on a collections.deque, in case you don't need to store your data in a list:

from collections import deque

def dequefilter(deck, condition):
    for _ in xrange(len(deck)):
        item = deck.popleft()
        if condition(item):
            deck.append(item)

deck = deque((1, 2, 3, 4, 5))
dequefilter(deck, lambda x: x > 2) # or operator.gt(2)
print deck
# deque([3, 4, 5])



回答2:


If you want to do this in place, just use

lst[:] = [i for i in lst if i > 3]

This won't be faster or save any memory, but it changes the object in place, if this is the semantics you need.




回答3:


Correcting @larsmans original solution, you could either do

i = 0
while i < len(lst):
    if lst[i] <= 3:
        del lst[i]
    else
        i += 1

or

i = len(lst)
while i > 0:
    if lst[i-1] <= 3:
        del lst[i-1]
    i -= 1

Reason is the "index shift" which happens with the del. If I del at a ceratin index, I have to re-examine that index because it now holds a different value.




回答4:


The lst[:] solution by @Sven Marnach is one option. You can also perform this operation in-place, using constant extra memory, with

>>> i = 0
>>> while i < len(lst):
...  if lst[i] <= 3:
...   del lst[i]
...  else:
...   i += 1
... 
>>> lst
[4, 5, 6]

... but this solution is not very readable and takes quadratic time due to all the element shifting involved.




回答5:


Because it's not needed.

lst[:] = [i for i in lst if i > 3]



回答6:


I think it's in place transformation;

lst = [1,2,3,4,5,6,7,8,9,10,11]
to_exclude = [8,4,11,9]
print 'lst == %s\nto_exclude == %s' % (lst,to_exclude)

for i in xrange(len(lst)-1,-1,-1):
    if lst[i] in to_exclude:
        lst.pop(i)

print '\nlst ==',lst

result

lst == [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
to_exclude == [8, 4, 11, 9]

lst == [1, 2, 3, 5, 6, 7, 10]


来源:https://stackoverflow.com/questions/8037455/how-to-modify-python-collections-by-filtering-in-place

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