Hello, Need some help in searching arrays (Im new to numpy) Is it possible to search a array, using another array considering order/sequence? x = np.array([1,2,3,4,5,6], np.int32) y = np.array([1,4,3,2,6,5], np.int32) query= np.array([1,2,3],np.int32) x versus query True y versus query False Tried with: np.searchsorted(x,query) > array([0, 1, 2]) np.searchsorted(y,query) > array([0, 1, 4]) Thanks  View this message in context: http://numpydiscussion.10968.n7.nabble.com/Arraysearchconsideringordert... Sent from the Numpydiscussion mailing list archive at Nabble.com.
On 19 December 2013 12:51, rootspin <andrei@ruivo.org> wrote:
Hello,
Need some help in searching arrays (Im new to numpy) Is it possible to search a array, using another array considering order/sequence?
x = np.array([1,2,3,4,5,6], np.int32)
y = np.array([1,4,3,2,6,5], np.int32)
query= np.array([1,2,3],np.int32)
x versus query True y versus query False
Tried with:
np.searchsorted(x,query) > array([0, 1, 2]) np.searchsorted(y,query) > array([0, 1, 4])
I'm not sure if I understand your problem do you mean that you want to find subarrays of an array the same way that e.g. "in" for strings tests for substrings:
'asd' in 'qweasdzxc' True
If so then perhaps this SO question has the answer you want: http://stackoverflow.com/questions/7100242/pythonnumpyfirstoccurrenceof... Oscar
On Thu, Dec 19, 2013 at 01:51:30PM +0000, Oscar Benjamin wrote:
Date: Thu, 19 Dec 2013 13:51:30 +0000 From: Oscar Benjamin <oscar.j.benjamin@gmail.com> To: Discussion of Numerical Python <numpydiscussion@scipy.org> Subject: Re: [Numpydiscussion] Array search considering order
On 19 December 2013 12:51, rootspin <andrei@ruivo.org> wrote:
Hello,
Need some help in searching arrays (Im new to numpy) Is it possible to search a array, using another array considering order/sequence?
x = np.array([1,2,3,4,5,6], np.int32)
y = np.array([1,4,3,2,6,5], np.int32)
query= np.array([1,2,3],np.int32)
x versus query True y versus query False
Tried with:
np.searchsorted(x,query) > array([0, 1, 2]) np.searchsorted(y,query) > array([0, 1, 4])
I'm not sure if I understand your problem do you mean that you want to find subarrays of an array the same way that e.g. "in" for strings tests for substrings:
'asd' in 'qweasdzxc' True
If so then perhaps this SO question has the answer you want: http://stackoverflow.com/questions/7100242/pythonnumpyfirstoccurrenceof...
Oscar _______________________________________________ NumPyDiscussion mailing list NumPyDiscussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpydiscussion
Thank you Oscar for your answer. Sorry if I was not clear enough. The SO question is alike what I want. However, in my problem, Im not sure if there will be only one occurence. What I do expect is: Given one array (big one), to retrieve indexes for "query array" occurences. Something like that: array1(big one)  1,2,3,4,5,6,7,1,2,3,4,5,7,8,9,1,2,3,4,5,6,7,8 array2(query)  1,2,3 array3(another query)  8,7,6 result (array1 versus array2)  0,7,15 (position for match) result (array1 versus array3)  no matches Thanks for the help.
On 19 December 2013 16:49, Andrei Rozanski <andrei@ruivo.org> wrote:
Sorry if I was not clear enough. The SO question is alike what I want. However, in my problem, Im not sure if there will be only one occurence. What I do expect is: Given one array (big one), to retrieve indexes for "query array" occurences. Something like that:
array1(big one)  1,2,3,4,5,6,7,1,2,3,4,5,7,8,9,1,2,3,4,5,6,7,8 array2(query)  1,2,3 array3(another query)  8,7,6
result (array1 versus array2)  0,7,15 (position for match) result (array1 versus array3)  no matches
If you look more closely at the SO question you'll see that it does answer your problem. The toprated answer shows how to get an boolean array indicating where the matches are. In your case that would be [True, False, False, False, False, False, False, True, False, False, False, False, False, False, False, True, False, False, False, False, False] Now just use the numpy.where function to get the indices of the True value from this array. Oscar
On Fri, Dec 20, 2013 at 11:19:49AM +0000, Oscar Benjamin wrote:
Date: Fri, 20 Dec 2013 11:19:49 +0000 From: Oscar Benjamin <oscar.j.benjamin@gmail.com> To: Discussion of Numerical Python <numpydiscussion@scipy.org> Subject: Re: [Numpydiscussion] Array search considering order
On 19 December 2013 16:49, Andrei Rozanski <andrei@ruivo.org> wrote:
Sorry if I was not clear enough. The SO question is alike what I want. However, in my problem, Im not sure if there will be only one occurence. What I do expect is: Given one array (big one), to retrieve indexes for "query array" occurences. Something like that:
array1(big one)  1,2,3,4,5,6,7,1,2,3,4,5,7,8,9,1,2,3,4,5,6,7,8 array2(query)  1,2,3 array3(another query)  8,7,6
result (array1 versus array2)  0,7,15 (position for match) result (array1 versus array3)  no matches
If you look more closely at the SO question you'll see that it does answer your problem. The toprated answer shows how to get an boolean array indicating where the matches are. In your case that would be
[True, False, False, False, False, False, False, True, False, False, False, False, False, False, False, True, False, False, False, False, False]
Now just use the numpy.where function to get the indices of the True value from this array.
Oscar _______________________________________________ NumPyDiscussion mailing list NumPyDiscussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpydiscussion
Hi Oscar, Thank you for the help. Indeed. My lack of knowledge on numpy functions didnt aloud my to get that. That works for me. best regards.
participants (3)

Andrei Rozanski

Oscar Benjamin

rootspin