简体   繁体   中英

Remove items from two lists based on value at index in either

I need to compare two lists and conditionally remove items from both based on the value of either item.

Input:

a = np.array( [1 , 2 , 3 , -1 , 5] )
b = np.array( [1 , -1 , 3 , 4 , 5] )

Required output:

a_filter = [1 , 3 , 5]
b_filter = [1 , 3 , 5]

I can easily get an array of list indexes where a "-1" value occurs using:

np.where( ( a == -1 ) | (b == -1 ) )[0]

But what is the most efficient way to delete these indexes from both list a and b? The only way I can think of is to reverse sort the identified index's and iter through both list removing them, however this seems quite inefficient.

List order is important as need to make confusion matrix, but don't want to compare -1 results.

You can use numpy.delete for this:

>>> a_filter = np.delete(a, np.where( ( a == -1 ) | (b == -1 ) )[0])
>>> a_filter
array([1, 3, 5])
>>> b_filter = np.delete(b, np.where( ( a == -1 ) | (b == -1 ) )[0])
>>> b_filter
array([1, 3, 5])

IIUC, you could use boolean indexing:

import numpy as np

a = np.array( [1 , 2 , 3 , -1 , 5] )
b = np.array( [1 , -1 , 3 , 4 , 5] )

mask = ~((a == -1) | (b == -1))

a_filtered = a[mask]
b_filtered = b[mask]

print(a_filtered)
print(b_filtered)

Output

[1 3 5]
[1 3 5]

Since you tag pandas

df=pd.DataFrame([a,b]).T
s=df[~df.eq(-1).any(1)].values
a_=s[:,0]
b_=s[:,1]
a_
Out[477]: array([1, 3, 5], dtype=int64)
b_
Out[478]: array([1, 3, 5], dtype=int64)

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