[英]Python check if object is in list of objects
我在Python中有一个对象列表。 然后我有另一个对象列表。 我想浏览第一个列表,看看是否有任何项目出现在第二个列表中。
我以为我可以做到
for item1 in list1:
for item2 in list2:
if item1 == item2:
print "item %s in both lists"
然而,这似乎不起作用。 虽然我这样做:
if item1.title == item2.title:
它工作正常。 我有比这更多的属性,所以不要真的想做一个大的if语句比较所有的属性,如果我不需要。
任何人都可以给我帮助或建议我可以做什么来找到两个列表中出现的对象。
谢谢
假设您的对象只有一个与相等相关的title
属性,您必须按如下方式实现__eq__
方法:
class YourObject:
[...]
def __eq__(self, other):
return self.title == other.title
当然,如果您有更多与平等相关的属性,那么您也必须包含这些属性。 您也可以考虑实现__ne__
和__cmp__
以保持一致的行为。
集合交叉将为此做。
>>> x=[1,2,3,4]
>>> y=[3,4,5,6]
>>> for i in set(x) & set(y):
... print "item %d in both lists" %i
...
item 3 in both lists
item 4 in both lists
查找出现在两个列表中的对象:
l1 = [1,2,3,4,5]
l2 = [3,4,5]
common = set(l1).intersection(set(l2))
将其与对象上的__eq__
实现相结合,如其他人所建议的那样。
您需要编写__eq__
函数来定义如何比较对象的相等性。 如果你想要排序,那么你应该有一个__cmp__
函数,并且根据__cmp__
实现__eq__
是最有意义的。
def __eq__(self, other):
return cmp(self, other) == 0
您可能还应该实现__hash__
,如果您计划将对象放入集合或字典中,那么您肯定应该这样做。 对象的默认__hash__
是id(),它有效地使所有对象都是唯一的(即唯一性不基于对象内容)。
我为一个类做了基类/接口,它进行了这种等价比较。 您可能会发现它很有用:
class Comparable(object):
def attrs(self):
raise Exception("Must be implemented in concrete sub-class!")
def __values(self):
return (getattr(self, attr) for attr in self.attrs())
def __hash__(self):
return reduce(lambda x, y: 37 * x + hash(y), self.__values(), 0)
def __cmp__(self, other):
for s, o in zip(self.__values(), other.__values()):
c = cmp(s, o)
if c:
return c
return 0
def __eq__(self, other):
return cmp(self, other) == 0
def __lt__(self, other):
return cmp(self, other) < 0
def __gt__(self, other):
return cmp(self, other) > 0
if __name__ == '__main__':
class Foo(Comparable):
def __init__(self, x, y):
self.x = x
self.y = y
def attrs(self):
return ('x', 'y')
def __str__(self):
return "Foo[%d,%d]" % (self.x, self.y)
def foo_iter(x):
for i in range(x):
for j in range(x):
yield Foo(i, j)
for a in foo_iter(4):
for b in foo_iter(4):
if a<b: print "%(a)s < %(b)s" % locals()
if a==b: print "%(a)s == %(b)s" % locals()
if a>b: print "%(a)s > %(b)s" % locals()
派生类必须实现attrs()
,它返回对象的属性的元组或列表,这些属性有助于其身份(即,使其成为现实的不变属性)。 最重要的是,代码正确处理具有多个属性的等效性,这是通常不正确的旧学校代码。
matches = [x for x in listA if x in listB]
请尝试以下方法:
list1 = [item1, item2, item3]
list2 = [item3, item4, item5]
for item in list1:
if item in list2:
print "item %s in both lists" % item
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.