[Numpy-discussion] Numpify this?

Robert Kern robert.kern@gmail....
Sun May 18 03:08:35 CDT 2008


On Sun, May 18, 2008 at 2:59 AM, Matt Crane <matt@snapbug.geek.nz> wrote:
> Sorry, I should have mentioned that no, the matching rows won't always
> be in the same position.

Okay, then it's just a little bit more complicated.


In [18]: from numpy import *
In [19]: a = array([[1, 10], [2, 20], [3, 30], [3, 40], [4, 50]])

In [20]: b = array([[2, 60], [1, 70], [5, 80], [6, 90], [7, 100], [3, 110]])

In [21]: m = a[:,0] == b[:,0][:,newaxis]

In [22]: m
Out[22]:
array([[False,  True, False, False, False],
       [ True, False, False, False, False],
       [False, False, False, False, False],
       [False, False, False, False, False],
       [False, False, False, False, False],
       [False, False,  True,  True, False]], dtype=bool)

In [23]: i, j = nonzero(m)

In [24]: i
Out[24]: array([0, 1, 5, 5])

In [25]: j
Out[25]: array([1, 0, 2, 3])

In [26]: column_stack([a[j,1], b[i,1]])
Out[26]:
array([[ 20,  60],
       [ 10,  70],
       [ 30, 110],
       [ 40, 110]])


-- 
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


More information about the Numpy-discussion mailing list