0

I have the following 2 arrays:

arr = np.array([[1, 2, 3, 4],
                [5, 6, 7, 8], 
                [7, 5, 6, 3],
                [2, 4, 8, 9]]

 ids = np.array([6, 5])

Each row in the array arr describes a 4-digit id, there are no redundant ids - neither in their values nor their combination. So if [1, 2, 3, 4] exists, no other combination of these 4 digits can exist. This will be important in a sec.

The array ids contains a 2-digit id, however the order is random. Now I need to go through each row of arr and look if this 2-digit partial id part of any 4-digit id. In this example ids fits to the 2nd and 3rd row from the top of arr.

My current solution with np.isin only works if the array ids has also a 4-digit row.

arr[np.isin(arr, ids).all(1)]

Changing all(1) to any(1) doesn´t do the trick either, because this way it would be enough if just one digit of ids is in one row of arr, however I need both values.

Does anyone have a compact solution?

1 Answer 1

1

Just need the boolean index to only accept values that are 2. When doing non-boolean operations like sum with boolean arrays, True and False values are interpreted as 1 and 0

arr[np.isin(arr, ids).sum(1) == 2]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.