matching elements of numeric arrays
Claudio Grondi
claudio.grondi at freenet.de
Thu Oct 13 20:03:28 EDT 2005
<daniel.neilson at gmail.com> wrote in
news:1129239096.340248.222440 at g49g2000cwa.googlegroups.com...
> I have two one-dimensional Numeric arrays, and I need to know the
> indices in the second array of elements from the first.
>
> so if i had:
>
> a=array([2,4,6])
> b=array([2,3,4,5,6])
>
> i want a function match that does this:
>
> >>> match(a,b)
> array([0,2,4])
>
> i have something that works, but it involves converting things to lists
> and using 'in', and it is rather slow. if someone could point me to a
> better solution, i would appreciate it.
I have no idea about Numeric array module, so I will use
the built-in Python one for it (how about the speed if
compared to Numeric arrays?):
from array import array
a = array('I', [2,4,6])
b = array('I', [2,3,4,5,6])
def match(a,b):
retVal = array('I')
for item in a:
retVal.append(b.index(item))
return retVal
print a
print b
print str(match(a,b))
Outputs:
array('I', [2L, 4L, 6L])
array('I', [2L, 3L, 4L, 5L, 6L])
array('I', [0L, 2L, 4L])
Claudio
More information about the Python-list
mailing list