【发布时间】:2018-01-13 16:58:43
【问题描述】:
如果有人能帮我解决这个问题(并解释发生了什么),我将不胜感激。
这行得通:
>>> from numpy import array
>>> a = array((2, 1))
>>> b = array((3, 3))
>>> l = [a, b]
>>> a in l
True
但这不是:
>>> c = array((2, 1))
>>> c in l
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
我想复制的行为是:
>>> x = (2, 1)
>>> y = (3, 3)
>>> l2 = [x, y]
>>> z = (2, 1)
>>> z in l2
True
请注意,上述内容也适用于可变对象:
>>> x = [2, 1]
>>> y = [3, 3]
>>> l2 = [x, y]
>>> z = [2, 1]
>>> z in l2
True
当然,知道:
>>> (a < b).all()
True
我尝试过(但失败了):
>>> (c in l).all()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
【问题讨论】:
标签: python numpy python-2.7