繁体   English   中英

Python 查找两个自定义类集之间的差异

[英]Python Find Difference Between Two Custom Class Sets

假设我有两个产品集 A 和 B,产品是我的自定义类。 我如何才能找到 A 组中的所有产品都符合以下条件?

a.link == b.link 和 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

使用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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM