我想檢查數組列表中存在的數組。我的問題可以歸結爲以下示例:數組在列表中的存在
>>> import numpy as np
>>> a = np.array([1,2,3])
>>> b = np.array([4,5,6])
>>> c = [a,b]
>>> c
[array([1, 2, 3]), array([4, 5, 6])]
>>> a in c
True
>>> np.array([1,2,3]) in c
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()
>>> d = np.array([1,2,3])
>>> d in c
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()
如何檢查數組列表中是否存在數組?這工作,當我使用的常規列表,我期望:
>>> a = [1,2,3]
>>> b = [4,5,6]
>>> c = [a,b]
>>> [1,2,3] in c
True
>>> [4,5,6] in c
True
>>> [1,2] in c
False
>>> d = [1,2,3]
>>> d in c
True
在python 3.5中,numpy 1.10.4,如果ndarray1中的第一個元素出現在ndarray2中,而ndarray1的其他元素完全忽略,則「ndarray1在ndarray2中」看起來會返回true – 2016-04-18 11:19:05
也就是說,這不會回答原始問題,並且事實上我不認爲在ndarrays上使用'in'關鍵字是爲了讓numpy開發者定義好行爲。 – 2016-04-18 11:25:10
請嘗試「c」中的np.array([1,99,-1]),例如... – 2016-04-18 11:36:50