Remove all occurrences of a value from a list?

后端 未结 23 1864
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-21 23:45

In Python remove() will remove the first occurrence of value in a list.

How to remove all occurrences of a value from a list?

This is w

相关标签:
23条回答
  • 2020-11-22 00:07

    About the speed!

    import time
    s_time = time.time()
    
    print 'start'
    a = range(100000000)
    del a[:]
    print 'finished in %0.2f' % (time.time() - s_time)
    # start
    # finished in 3.25
    
    s_time = time.time()
    print 'start'
    a = range(100000000)
    a = []
    print 'finished in %0.2f' % (time.time() - s_time)
    # start
    # finished in 2.11
    
    0 讨论(0)
  • 2020-11-22 00:10

    Numpy approach and timings against a list/array with 1.000.000 elements:

    Timings:

    In [10]: a.shape
    Out[10]: (1000000,)
    
    In [13]: len(lst)
    Out[13]: 1000000
    
    In [18]: %timeit a[a != 2]
    100 loops, best of 3: 2.94 ms per loop
    
    In [19]: %timeit [x for x in lst if x != 2]
    10 loops, best of 3: 79.7 ms per loop
    

    Conclusion: numpy is 27 times faster (on my notebook) compared to list comprehension approach

    PS if you want to convert your regular Python list lst to numpy array:

    arr = np.array(lst)
    

    Setup:

    import numpy as np
    a = np.random.randint(0, 1000, 10**6)
    
    In [10]: a.shape
    Out[10]: (1000000,)
    
    In [12]: lst = a.tolist()
    
    In [13]: len(lst)
    Out[13]: 1000000
    

    Check:

    In [14]: a[a != 2].shape
    Out[14]: (998949,)
    
    In [15]: len([x for x in lst if x != 2])
    Out[15]: 998949
    
    0 讨论(0)
  • 2020-11-22 00:11

    If you didn't have built-in filter or didn't want to use extra space and you need a linear solution...

    def remove_all(A, v):
        k = 0
        n = len(A)
        for i in range(n):
            if A[i] !=  v:
                A[k] = A[i]
                k += 1
    
        A = A[:k]
    
    0 讨论(0)
  • You can use slice assignment if the original list must be modified, while still using an efficient list comprehension (or generator expression).

    >>> x = [1, 2, 3, 4, 2, 2, 3]
    >>> x[:] = (value for value in x if value != 2)
    >>> x
    [1, 3, 4, 3]
    
    0 讨论(0)
  • 2020-11-22 00:18

    No one has posted an optimal answer for time and space complexity, so I thought I would give it a shot. Here is a solution that removes all occurrences of a specific value without creating a new array and at an efficient time complexity. The drawback is that the elements do not maintain order.

    Time complexity: O(n)
    Additional space complexity: O(1)

    def main():
        test_case([1, 2, 3, 4, 2, 2, 3], 2)     # [1, 3, 3, 4]
        test_case([3, 3, 3], 3)                 # []
        test_case([1, 1, 1], 3)                 # [1, 1, 1]
    
    
    def test_case(test_val, remove_val):
        remove_element_in_place(test_val, remove_val)
        print(test_val)
    
    
    def remove_element_in_place(my_list, remove_value):
        length_my_list = len(my_list)
        swap_idx = length_my_list - 1
    
        for idx in range(length_my_list - 1, -1, -1):
            if my_list[idx] == remove_value:
                my_list[idx], my_list[swap_idx] = my_list[swap_idx], my_list[idx]
                swap_idx -= 1
    
        for pop_idx in range(length_my_list - swap_idx - 1):
            my_list.pop() # O(1) operation
    
    
    if __name__ == '__main__':
        main()
    
    0 讨论(0)
提交回复
热议问题