[英]checking presence of lists in a list of lists in python
I need to check the presence of a list in list of lists. 我需要检查列表列表中是否存在列表。 The problem that i face is that my list is composed of class objects and therefore i am not able to do this using the normal "if list in list_of_lists" method
我面临的问题是我的列表由类对象组成,因此我无法使用常规的“ if list_of_lists”方法来执行此操作
the relevant part of my code is as follows 我的代码的相关部分如下
for ind in feasible_pop_comp:
for other in feasible_pop_comp:
if ind.Type!= other.Type:
comp=[ind,other]
if comp not in self.candidate.list #does not work even with .any() or .all() included
dombool=self.compare_typematch(ind, other)
if (dombool==0):
replace=self.check_distance(ind.point,other.point)
if replace:
if(ind<other):
feasible_pop_comp.remove(other)
else:
feasible_pop_comp.remove(ind)
else:
self.candidate_list.append(comp)
My class already has inbuilt commands to check structural similarities with other class objects(read equality) 我的班级已经具有内置命令来检查与其他班级对象的结构相似性(读取相等性)
def __eq__(self, other):
return self.point == other.point# as all other parameters are derived from analysing the point, this equivalence is sufficient
Traceback is as follows: 追溯如下:
if comp not in self.candidate_list:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Failed !
it loop enters for the first time when the self.candidate_list is empty 当self.candidate_list为空时,它第一次进入循环
As you might notice from my unpythonic code, I am relatively new to python. 您可能会从我的非Python代码中注意到,我对python还是比较陌生的。 Thanks in Advance.
提前致谢。
Have you tried overriding the __contains__
method for your container class? 您是否尝试为容器类重写
__contains__
方法? That way you could get the in
operator to work. 这样,您可以使
in
运算符起作用。
But after all, I'm not really sure whether I did understand all parts of your problem... 但毕竟,我不确定我是否确实了解您问题的所有方面...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.