python - Obtain one number from numpy subarrays given its peer -
I have an array, which is similar to the number of numbers (size 2 of the form):
pairs = NP Array (Array ([[1, 2], [5, 12], [9, 33], [9, 1], [34,7]])
Like:
nums = np.array ([1,12,9])
What do I have to do is associate the numbers in the numbers array exactly The result should be
result = np.array ([2, 5, 33, 9, 1])
How can I get Is it using numpy functions? I'm using lazy comparisons (actually using lists), for each element in the nums array, I check that it is in everybody and I store values.
If the order of elements does not matter, you can use it easily:
& gt; & Gt; & Gt; Np.concatenate (pairs [:, 1] [np.in1d (pairs [:, 0], numbers)], pairs [:, 0] [np.in1d (pairs [:, 1], number) ])) Array ([2, 33, 1, 5, 9])
Edit:
To protect the order For, you can use it:
& gt; & Gt; & Gt; Pairs [np.in1d (pairs, numbers). Your ((pairs.shape [0], 2)) [,: [1,0]]] array ([2, 5, 33, 9, 1])
Comments
Post a Comment