简体   繁体   中英

Remove elements from a list that occur in another list and return their indices

This list , for example:

my_list = ['a', 'd', 'a', 'd', 'c','e']
words_2_remove = ['a', 'c']

the output should then be:

my_list = ['d', 'd', 'e']
loc = [0, 2, 4]

I am currently using this:

loc = []    
for word in my_list:  
    if word in words_2_remove:
         loc.append( my_list.index(word) )
         my_list.remove(word)

Is there a better alternative?

Do two list-comprehensions:

my_list =['a', 'd', 'a', 'd', 'c','e']
words_2_remove = ['a', 'c']

loc = [i for i, x in enumerate(my_list) if x in words_2_remove]

my_list = [x for x in my_list if x not in words_2_remove]

print(my_list) # ['d', 'd', 'e']
print(loc)     # [0, 2, 4]

For bigger arrays using NumPy will be more efficient:

import numpy as np


my_list = np.array(['a', 'd', 'a', 'd', 'c','e'])
words_2_remove = np.array(['a', 'c'])

mask = np.isin(my_list, words_2_remove, invert=True)
# mask will be [False  True False  True False  True]
loc = np.where(~mask)[0]

print(loc)
>>> [0 2 4]

print(my_list[mask])
>>> ['d' 'd' 'e']

And it's also pretty easy to get the complement of the loc indices:

print(np.where(mask)[0])
>>> [1 3 5]

Timings:
Comparing with list comprehensions version from @Austin.
For original arrays:

my_list = np.array(['a', 'd', 'a', 'd', 'c','e'])
words_2_remove = np.array(['a', 'c'])

%%timeit
mask = np.isin(my_list, words_2_remove, invert=True)
loc = np.where(~mask)[0]
>>> 11 µs ± 53.7 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

my_list =['a', 'd', 'a', 'd', 'c','e']
words_2_remove = ['a', 'c']

%%timeit
loc = [i for i, x in enumerate(my_list) if x in words_2_remove]
res = [x for x in my_list if x not in words_2_remove]
>>> 1.31 µs ± 7.17 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

And for big arrays:

n = 10 ** 3
my_list = np.array(['a', 'd', 'a', 'd', 'c','e'] * n)
words_2_remove = np.array(['a', 'c'])

%%timeit
mask = np.isin(my_list, words_2_remove, invert=True)
loc = np.where(~mask)[0]
>>> 114 µs ± 906 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

my_list =['a', 'd', 'a', 'd', 'c','e'] * n
words_2_remove = ['a', 'c']

%%timeit
loc = [i for i, x in enumerate(my_list) if x in words_2_remove]
res = [x for x in my_list if x not in words_2_remove]
>>> 841 µs ± 677 ns per loop (mean ± std. dev. of 7 runs, 1000 loops each)

Depending on the use case, you can choose what fits better.


Further reading:

Docs on np.isin : https://docs.scipy.org/doc/numpy-1.15.1/reference/generated/numpy.isin.html
Converting boolean mask array to indices: How to turn a boolean array into index array in numpy
Docs on np.where : https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.where.html
More on indexing with NumPy: https://docs.scipy.org/doc/numpy-1.15.1/reference/arrays.indexing.html

Using list comprehension and enumerate

loc = [idx for idx, item in enumerate(my_list) if item in words_2_remove]
my_list = [i for i in my_list if i not in words_2_remove]

Or using filter :

my_list = list(filter(lambda x: x not in words_2_remove, my_list))

Expanded Explanation:

loc = []
new_my_list = []
for idx, item in enumerate(my_list):
    if item in words_2_remove:
        loc.append(idx)
    else:
        new_my_list.append(item)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM