efficent test for array with only one value?
Robert Kern
rkern at ucsd.edu
Mon Jan 19 17:19:27 EST 2004
Kyler Laird wrote:
> I'm trying to discover if there's an efficient way to determine
> if all of the values of a Numeric array are the same. In C, I
> would search from the second value, checking each against the
> first value. The first one that doesn't match would trigger a
> False return value. If the loop completes, True would be
> returned.
>
> Looking through array functions, I'm not finding anything like
> that. I'm imagining that there should be something like an
> equal function (Is that Lisp I'm recalling?) that performs
> a[0,0] == a[0,1] == a[0,2] == ...
> and returns False as soon as it is known. I don't see that.
>
> I can, of course, iterate through all of the values, but it
> seems like there should be an efficient built-in function to do
> it.
Python 2.3.3 (#1, Dec 28 2003, 00:16:29)
[GCC 3.3 20030304 (Apple Computer, Inc. build 1495)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from Numeric import *
>>> a = ones((3,5))
>>> equal.reduce(a.flat)
1
>>> a[0,3] = 0
>>> equal.reduce(a.flat)
0
>>>
Ufuncs are wonderful things.
> Thank you.
>
> --kyler
--
Robert Kern
rkern at ucsd.edu
"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
More information about the Python-list
mailing list