Numeric don't compare arrays correctly

Alexander Schmolck a.schmolck at gmx.net
Fri Jun 6 16:08:52 EDT 2003


Maurix <maurix78_remove_this_ at wanadoo.es> writes:

> Hi to all,
> I'm new in python world and in this group too so, nice to meet you all ;-)
> I have a problem with Numeric: (linux, python2.2, numeric 21.0)
> 
> It seem that don't compare correctly arrays.
> 
> See at this session:
> 
>  >>> a=array([1.1,2.2,3.3,4.4,5.5])
>  >>> print 2.<a<4.

This is just a python wart, reflecting the unhappy interaction between two
sometimes convinient but somewhat messy language features.

Feature 1:

a < b < c in most programming languages is the same as (a < b) < c. Python
instead treats such chained comparisons specially to make them behave as is
typical in (the somewhat messy) standard math notation.

So ``1 < 3 < 4`` is False and not True, because it is treated as 
``1 < 3 and 3 < 4``. 

Feature 2:

Rich comparisons. a > b can return *anything*, not just 0/False 1/True. In
particular it is used. In particular, Numeric returns elementwise comparison
arrays for ``array1 < array2``. My personal feeling is that allowing this was
a bad idea one symptom is that it clashes with Feature 1 (
``array1 < array2 < array3`` = ``array1 < array2 and array 2< array 3`` =
``array` < array 2`` if at least one element in array1 is smaller than in
array 2 = not what you most likely want).


'as




More information about the Python-list mailing list