[Numpy-discussion] Numpify this?

Matt Crane matt at snapbug.geek.nz
Sun May 18 03:59:43 EDT 2008


On Sun, May 18, 2008 at 7:19 PM, Robert Kern <robert.kern at gmail.com> wrote:
> Are the matching rows always going to be the same row in each? I.e.
> you want rows i such that a[i,0]==b[i,0] rather than trying to find
> all i,j such that a[i,0]==b[j,0]?
>
> If so, then I would do the following:
>
>
> In [1]: from numpy import *
>
> In [2]: a = array([[2834, 1], [3282, 3], [6850, 2], [9458, 2]])
>
> In [3]: b = array([[2834, 3], [3282, 5], [4444, 5], [9458, 3], [9999,
> 4], [11111,
>   ...: 5], [12345, 1]])
>
> In [4]: minlength = min(a.shape[0], b.shape[0])
>
> In [5]: matching = nonzero(a[:minlength,0] == b[:minlength,0])[0]
>
> In [6]: matching
> Out[6]: array([0, 1, 3])
>
> In [7]: column_stack([a[matching,1], b[matching,1]])
> Out[7]:
> array([[1, 3],
>       [3, 5],
>       [2, 3]])
>
> --
> Robert Kern
>
> "I have come to believe that the whole world is an enigma, a harmless
> enigma that is made terrible by our own mad attempt to interpret it as
> though it had an underlying truth."
>  -- Umberto Eco
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>

Sorry, I should have mentioned that no, the matching rows won't always
be in the same position.



More information about the NumPy-Discussion mailing list