Skip to content
Advertisement

Check if any element of a list is in a matrix?

I have a small list:

JavaScript

and I have a matrix too. for example:

JavaScript

And I need to determine if any of the elements of l ist2 of this array are not in the matrix2. I try to use np.isin() :

JavaScript

But this code only shows if there is the elements in the matrix or not completely. But I need this output:

JavaScript

As you can see in the output, the spaces are important too. For example, ‘hi’ without space before and after it is not something that I want it. I need the list by considering spaces between the set of characters.

Could anyone help me to solve this problem?

Advertisement

Answer

You have to loop over the matrix and then over each row of the matrix. Then you have a single string that you have to split to get the individual parts. Now you can check if any of this parts is in list2.

list2 = [‘hi’, ‘ma’, ‘ja’]

JavaScript

The result is

JavaScript

A minor change that takes the additional constraint of an existing space into account.

JavaScript

This will give you

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement