假设我有两个产品组A和B,产品是我的自定义类。 如何找到集A中的所有产品符合以下标准?
a.link == b.link and a.date!= b.date
A = set([Product('link1', '02-08-2018'), Product('link2', '01-01-2018'), Product('link3', '02-02-2018')])
B = set([Product('link1', '01-08-2018'), Product('link2', '01-01-2018'), Product('link4', '02-02-2018')])
# HOW?? I want get Product('link1', '02-08-2018') and Product('link3', '02-02-2018') back here
result = A - B
class Product:
def __init__(self, data):
self.link= data['link']
self.date= data['date']
def __hash__(self):
return hash(self.link+self.date)
def __eq__(self, other):
return self.link == other.link and self.date == other.date
答案 0 :(得分:1)
使用A.difference(B)
class Product(object):
def __init__(self, link, date):
self.link= link
self.date= date
def __hash__(self):
return hash(self.link+self.date)
def __eq__(self, other):
return self.link == other.link and self.date == other.date
A = set([Product('link1', '02-08-2018'), Product('link2', '01-01-2018'), Product('link3', '02-02-2018')])
B = set([Product('link1', '01-08-2018'), Product('link2', '01-01-2018'), Product('link4', '02-02-2018')])
result = A.difference(B)