Search array in array
Hello, I'm wondering if there is a fast way to solve the following problem. I have two arrays: A = [[ 4, 9, 10], [ 7, 4, 17], [12, 21, 14], [12, 24, 11], [18, 21, 3], [16, 3, 7], [17, 21, 5], [24, 3, 14]] B = [[17, 5], [14, 21]] I need to search rows of A that contain elements of each row of B regardless of the order of the elements in B. The searched results is: [2, 6] . Thanks. Nicola -- _____________________________________________________________________ Nicola Creati Istituto Nazionale di Oceanografia e di Geofisica Sperimentale - OGS IRI (Ricerca Teconologica e Infrastrutture) Department B.go Grotta Gigante - Brisciki 42/c 34010 Sgonico - Zgonik (TS) - Italy Tel. +39-0402140213 Fax +39-040327307
Nicola Creati <ncreati <at> inogs.it> writes:
I need to search rows of A that contain elements of each row of B regardless of the order of the elements in B.
I don't know how fast this is, but it is fairly short: C = (A[..., np.newaxis, np.newaxis] == B) rows = (C.sum(axis=(1,2,3)) >= B.shape[1]).nonzero()[0]
C = (A[..., np.newaxis, np.newaxis] == B) rows = (C.sum(axis=(1,2,3)) >= B.shape[1]).nonzero()[0] Hello,
On 07/29/2013 10:27 PM, Gabe Schwartz wrote: thank you, it's not fast but really nice. Nicola -- _____________________________________________________________________ Nicola Creati Istituto Nazionale di Oceanografia e di Geofisica Sperimentale - OGS IRI (Ricerca Teconologica e Infrastrutture) Department B.go Grotta Gigante - Brisciki 42/c 34010 Sgonico - Zgonik (TS) - Italy Tel. +39-0402140213 Fax +39-040327307
participants (2)
-
Gabe Schwartz
-
Nicola Creati