检查python列表中是否存在列表

问题描述:

我需要检查列表列表中是否存在列表。我面临的问题是,我的名单是由类的对象,所以我不能够做到这一点使用正常 “在list_of_lists如果名单”的方法检查python列表中是否存在列表

我的代码的相关部分如下

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) 

我的类已经有内置的命令检查结构相似性与其它类的对象(读平等)

def __eq__(self, other): 
    return self.point == other.point# as all other parameters are derived from analysing the point, this equivalence is sufficient 

回溯如下:

首次
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 ! 

它循环进入当self.candidate_list是空

正如你可能从我的unpythonic代码注意,我是比较新的蟒蛇。 在此先感谢。

+0

“我的类已经有内置的命令检查与其它类对象的结构相似” inspectorG4dget 2012-02-10 16:47:40

+0

我似乎并没有工作! – Sachiros 2012-02-10 16:48:32

+1

向我们展示您的'__eq__'方法和回溯。 – senderle 2012-02-10 16:49:49

你有没有尝试覆盖你的容器类的__contains__方法? 这样你可以让in运营商工作。

但毕竟,我真的不知道我是否确实了解你的问题的所有部分...

+0

这确实奏效。谢谢加载! – Sachiros 2012-02-10 17:42:53

+0

对不起新手来论坛。感谢您指出 – Sachiros 2012-02-10 17:57:24