[Tutor] Finding a row match within a numpy array
bob gailer
bgailer at alum.rpi.edu
Mon Aug 13 21:50:37 CEST 2007
bob gailer wrote:
> Andy Cheesman wrote:
>
>> Dear nice people
>>
>> I'm trying to match a row (b) within a large numpy array (a). My most
>> successful attempt is below
>>
>> hit = equal(b, a)
>> total_hits = add.reduce(hit, 1)
>> max_hit = argmax(total_hits, 0)
>> answer = a[max_hit]
>>
>> where ...
>> a = array([[ 0, 1, 2, 3],
>> [ 4, 5, 6, 7],
>> [ 8, 9, 10, 11],
>> [12, 13, 14, 15]])
>>
>> b = array([8, 9, 10, 11])
>>
>>
>>
>> I was wondering if people could suggest a possible more efficient route
>> as there seems to be numerous steps.
>>
> As an APL programmer I appreciate your approach.
> I'm not a numpy expert. I'm assuming one can apply *for* to an array to
> iterate thru the rows. Try this:
>
>
Let's fix that a bit (assumes 0 origin indexing):
> for answer, row in enumerate(a):
> if row == b:
> break
> else:
> # do something to handle no match
>
> Cheers,
> nice person
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
More information about the Tutor
mailing list