[英]Check if list does not contain list
Firstly, sorry if this is in fact a duplicate - I have spent the last three hours attempting to solve this problem and haven't been able to find any solution. 首先,很抱歉,如果这实际上是重复的操作,我已经花了最后三个小时来尝试解决此问题,但找不到任何解决方案。
I am using lists to represent coordinates as [x, y]
. 我使用列表将坐标表示为
[x, y]
。 I want to find out if a list of coordinates does not contain a specified coordinate. 我想找出坐标列表是否不包含指定坐标。 For example, if I have the list of coordinates
[[3.3, 4.4], [5.5, 6.6]]
and the coordinate [1.1, 2.2]
, I want a return of True
, because the coordinate is not in the list of coordinates. 例如,如果我有坐标
[[3.3, 4.4], [5.5, 6.6]]
和坐标[1.1, 2.2]
,则我想返回True
,因为坐标不在坐标列表中。
It may be worth noting that the list of coordinates is generated using the OpenCV functions cv2.findContours()
, cv2.minAreaRect()
and finally cv2.boxPoints()
which results in a list of lists. 可能值得注意的是,坐标列表是使用OpenCV函数
cv2.findContours()
, cv2.minAreaRect()
以及最终使用cv2.boxPoints()
,从而生成了列表列表。 These coordinates are stored in a dict and accessed from there; 这些坐标存储在字典中并从那里访问; calling a
print()
of the coordinates gives me the coordinates in the format [array([3.3, 4.4], dtype=float32), array([5.5, 6.6], dtype=float32)]
as opposed to the format [[3.3, 4.4], [5.5, 6.6]]
which is given when I print()
the coordinates straight after finding them with cv2.boxPoints()
. 调用坐标的
print()
得到格式为[array([3.3, 4.4], dtype=float32), array([5.5, 6.6], dtype=float32)]
的坐标,而不是格式[[3.3, 4.4], [5.5, 6.6]]
cv2.boxPoints()
[[3.3, 4.4], [5.5, 6.6]]
,当我使用cv2.boxPoints()
找到坐标后,直接print()
坐标时给出。
I have tried to use the answer to this question but I get the error ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
我尝试使用此问题的答案,但出现错误
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
. ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
。
The code for that attempt looks like this: 该尝试的代码如下所示:
for coordinate in box:
if coordinate not in specialCoordinates:
# operations go here
I then attempted to use the answer to this question about a.all() but I get the same error. 然后,我尝试使用有关a.all()的问题的答案,但出现相同的错误。
The code for that attempt looks like this: 该尝试的代码如下所示:
for coordinate in box:
if not all(coordinate == special for special in specialCoordinates):
# operations go here
I also tried this: 我也试过这个:
for coordinate in box:
if all(coordinate != special for special in specialCoordinates):
# operations go here
The format mentioned above if coordinate not in specialCoordinates
works when I try the following in the Python 2.7 interpreter: 当我在Python 2.7解释器中尝试以下操作时,
if coordinate not in specialCoordinates
中, if coordinate not in specialCoordinates
上述格式if coordinate not in specialCoordinates
起作用:
Python 2.7.15 (v2.7.15:ca079a3ea3, Apr 30 2018, 16:30:26) [MSC v.1500 64 bit (AMD64)] on win32 在Win32上的Python 2.7.15(v2.7.15:ca079a3ea3,2018年4月30日,16:30:26)[MSC v.1500 64位(AMD64)]
Type "help", "copyright", "credits" or "license" for more information. 键入“帮助”,“版权”,“信用”或“许可证”以获取更多信息。
>>> a = [[3.3, 4.4], [5.5, 6.6]] >>> a = [[3.3,4.4],[5.5,6.6]]
>>> b = [1.1, 2.2] >>> b = [1.1,2.2]
>>> b not in a >>> b不在a中
True 真正
The main problem here is that the numpy array elements in the list have precision associated with them for example: a = [np.array([3.3, 4.4], dtype='float32'), np.array([5.5, 6.6], dtype='float32')]
Which is equivalent to: [array([ 3.29999995, 4.4000001 ], dtype=float32), array([ 5.5 , 6.5999999], dtype=float32)]
这里的主要问题是列表中的numpy数组元素具有与之关联的精度,例如:
a = [np.array([3.3, 4.4], dtype='float32'), np.array([5.5, 6.6], dtype='float32')]
等效于: [array([ 3.29999995, 4.4000001 ], dtype=float32), array([ 5.5 , 6.5999999], dtype=float32)]
Hence, if you look for [3.3, 4.4] in a, it won't be there so you might want to play around with the precision or do a bit of casting. 因此,如果您在a中寻找[3.3,4.4],它就不会存在,因此您可能想提高精度或进行一些转换。
import numpy as np
#a = [array([3.3, 4.4]), array([5.5, 6.6])]
a = np.array([i.tolist() for i in a])
def if_not_in_a(A, B):
for i in A:
if np.linalg.norm(i - B) == 0:
#you can use tolerance like < 1e-2 to avoid floating point complicacy
return False
return True
print(if_not_in_a(a, np.array([1.1, 2.2])))
#prints True
print(if_not_in_a(a, np.array([3.3, 4.4])))
#prints False
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.