Search code examples
pythonlistmasked-array

Deleting elements from a list if that value is masked in another list


I have imported data that has the format of a numpy masked array of incrementing integers. The masked elements are irregular and not repeating, e.g printing it yields:

masked = [0,1,--,3,--,5,6,--,--,9,--]

And I have another list of incrementing numbers that doesn't start from zero, and has irregular gaps and is a different size from masked:

data = [1,3,4,6,7,9,10]

I want to remove any element of data if its value is a masked element in masked

So that I get:

result = [1,3,6,9]

As 4, 7 and 10 were masked values in masked.

I think my pseudocode should look something like:

for i in len(masked):
    if masked[i] = 'masked' && data[i] == [i]:
        del data[i]

But I'm having trouble reconciling the different lengths and mis-matched indices of the two arrays,

Thanks for any help!


Solution

  • Make sure data is an array:

    data = np.asarray(data)
    

    Then:

    data[~masked.mask[data]]
    

    This will be extremely fast, though it does assume that your masked array contains all numbers from 0 to at least max(data).