Python 2.7 为什么';numpy阵列上的t链(间隔)比较工作?

Python 2.7 为什么';numpy阵列上的t链(间隔)比较工作?,python-2.7,numpy,Python 2.7,Numpy,a

a
是Python中的一种,它似乎可以在定义了适当比较运算符的对象上工作,但不能在numpy数组上工作。为什么?

import numpy as np

class ContrarianContainer(object):
    def __init__(self, x):
        self.x = x
    def __le__(self, y):
        return not self.x <= y
    def __lt__(self, y):
        return not self.x < y
    def __ge__(self, y):
        return not self.x >= y
    def __gt__(self, y):
        return not self.x > y
    def __eq__(self, y):
        return not self.x == y
    def __ne__(self, y):
        return not self.x != y

numlist = np.array([1,2,3,4])
for n in numlist:
    print 0 < n < 3.5
for n in numlist:
    print 0 > ContrarianContainer(n) > 3.5
print 0 < numlist < 3.5
将numpy导入为np
类反向容器(对象):
定义初始化(self,x):
self.x=x
定义(自我,y):
返回非self.x=y
定义(自我,y):
返回非self.x>y
定义(自我,y):
返回非self.x==y
定义(自我,y):
返回非self.x!=Y
numlist=np.array([1,2,3,4])
对于numlist中的n:
打印0ContrarianContainer(n)>3.5
打印0
这张照片是:

True
True
True
False
True
True
True
False
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-187-277da7750148> in <module>()
      4 for n in numlist:
      5     print 0 < n < 3.5
----> 6 print 0 < numlist < 3.5

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
True
真的
真的
假的
真的
真的
真的
假的
---------------------------------------------------------------------------
ValueError回溯(最近一次调用上次)
在()
4对于numlist中的n:
5打印06打印0
0
相当于:

(0 < numlist) and (numlist < 3.5)
(0
除了
numlist
只计算一次之外

两个结果之间的隐式
导致错误

0
相当于:

(0 < numlist) and (numlist < 3.5)
(0
除了
numlist
只计算一次之外

两个结果之间的隐式
导致了错误

,因此文档说:

形式上,如果a,b,c,…,y,z是表达式,op1,op2,…,opN是比较运算符,那么a op1 b op2 c。。。y opN z等价于a op1 b和b op2 c,并且。。。y opN z,但每个表达式最多计算一次

(但在这两种情况下,当发现x 对于标量

In [20]: x=5
In [21]: 0<x<10
Out[21]: True
In [22]: 0<x and x<10
Out[22]: True
当我们使用
0
形式上,如果a,b,c,…,y,z是表达式,op1,op2,…,opN是比较运算符,那么a op1 b op2 c。。。y opN z等价于a op1 b和b op2 c,并且。。。y opN z,但每个表达式最多计算一次

(但在这两种情况下,当发现x 对于标量

In [20]: x=5
In [21]: 0<x<10
Out[21]: True
In [22]: 0<x and x<10
Out[22]: True

当我们使用
0
运算符来返回布尔值以外的任何值,而
&
运算符可以返回任何值wants@Eric你能在回答中详细说明为什么会这样吗?这就是我要找的信息。哦,等等——是不是因为
运算符不是运算符,而是带有短路求值的关键字,所以不能覆盖它?@JasonS:是的,它有短路求值这一事实是最主要的问题。注意,有一个允许重载这些。。。这是因为不能重写
运算符来返回布尔值以外的任何值,而
&
运算符可以返回任何值wants@Eric你能在回答中详细说明为什么会这样吗?这就是我要找的信息。哦,等等——是不是因为
运算符不是运算符,而是带有短路求值的关键字,所以不能覆盖它?@JasonS:是的,它有短路求值这一事实是最主要的问题。注意,有一个允许重载这些
In [26]: (0<x)
Out[26]: array([ True,  True,  True], dtype=bool)

In [30]: np.array([True, False]) or True
...
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
In [33]: if np.array([True, False]): print('yes')
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
In [34]: (0<x) & x<10
Out[34]: array([ True,  True,  True], dtype=bool)
In [35]: f = np.vectorize(lambda x: 0<x<10, otypes=[bool])
In [36]: f(x)
Out[36]: array([ True,  True,  True], dtype=bool)
In [37]: f([-1,5,11])
Out[37]: array([False,  True, False], dtype=bool)
In [39]: 0 < [-1,5,11]
TypeError: unorderable types: int() < list()
In [44]: 0 < x & x<10
ValueError ...

In [45]: (0 < x) & x<10
Out[45]: array([ True,  True,  True], dtype=bool)

In [46]: 0 < x & (x<10)
Out[46]: array([False,  True, False], dtype=bool)

In [47]: 0 < (x & x)<10
ValueError...
In [53]: x=2
In [54]: 3<x<np.arange(4)
Out[54]: False
In [55]: 1<x<np.arange(4)
Out[55]: array([False, False, False,  True])
In [56]: 3<x<[1,2,3]
Out[56]: False
In [57]: 1<x<[1,2,3]
Traceback (most recent call last):
  File "<ipython-input-57-e7430e03ad55>", line 1, in <module>
    1<x<[1,2,3]
TypeError: '<' not supported between instances of 'int' and 'list'