Delete specific values in 2-Dimension array - Numpy
Delete specific values in 2-Dimension array - Numpy
import numpy as np
I have two arrays of size n (to simplify, I use in this example n = 2):
A = array([[1,2,3],[1,2,3]])
B has two dimensions with n time a random integer: 1, 2 or 3.
Let's pretend:
B = array([[1],[3]])
What is the most pythonic way to subtract B from A in order to obtain C, C = array([2,3],[1,2])
?
C = array([2,3],[1,2])
I tried to use np.subtract
but due to the broadcasting rules I do not obtain C. I do not want to use mask or indices but element's values. I also tried to use np.delete
, np.where
without success.
Thank you.
np.subtract
np.delete
np.where
A
In my case, there is no duplicate. But if there is let 's say substract the first iteration.
– Tousalouest
Sep 14 '18 at 13:35
Check this link, hope it will help you out stackoverflow.com/questions/42557558/…
– JON
Sep 14 '18 at 13:36
And what if no match is found? For ex. :
B = array([[5],[3]])
?– Divakar
Sep 14 '18 at 13:38
B = array([[5],[3]])
In my case, there will alway be a match.
– Tousalouest
Sep 14 '18 at 13:45
1 Answer
1
This might work and should be quite Pythonic:
dd=[[val for val in A[i] if val not in B[i]] for i in xrange(len(A))]
Thank you. It does work with 'range' instead of 'xrange'.
– Tousalouest
Sep 14 '18 at 13:44
I am still using Python2.X, therefore
xrange
... Glad it worked!– msi_gerva
Sep 14 '18 at 13:46
xrange
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
What if there are duplicates in any row of
A
?– Divakar
Sep 14 '18 at 13:32