对象之间的比较,但不是对象数组之间的比较。

问题描述 投票:0回答:1

所以,我的问题是,当比较对象从类中的 House,一个对一个。


    def __init__(self, door: str, address: str, rooms: List[room],
                 garaje=None, number=None):
        self.door = door
        self.address = address
        # ensure and deserialize if needed the list of rooms
        try:
            if all(isinstance(room, dict) for room in rooms):
                rooms = list(map(room.from_json, rooms))
                # deserialize rooms
            assert all(isinstance(room, Room) for room in rooms)
        except AssertionError as e:
            logger.error("Rooms incorrect")
            raise e
        self.rooms = rooms
        self.garaje = garaje
        self.number = number

    def __eq__(self, other):
        '''
        Two houses instances will be equal when at least following values match:
         door,
         address
         rooms
        '''
        if not isinstance(other, House):
            return NotImplemented
        return (self.door == other.door and
                self.address == other.address and
                self.rooms == other.rooms)

    def __hash__(self):
        return hash((self.door, self.address, self.rooms))


    @classmethod
    def from_json(cls, data):
        return cls(**data)

该类 House 包含一个基于Room Class的房间对象列表。

class Room():

    def __init__(self, name: str, description: str, windows: list = None, bahtroom: bool = True,
                 , room_id: int = None, house_number: int = None):
        self.name = name
        self.description = description
        self.windows = windows
        self.bathroom = bathroom
        self.room_id = room_id
        self.house_number = house_number

    def __eq__(self, other):
        """Two room instances will be equal when at least following values match
         name,
         description,
         windows,
         bathroom
        """

        if not isinstance(other, Room):
            return NotImplemented

        return (self.name == other.name and
                self.description == other.description and
                self.windows == other.windows and
                self.bathroom == other.bathroom 
                )

    def __hash__(self):
        return hash((self.name, self.descritpion, self.windows, self.bathroom))

    @classmethod
    def from_json(cls, data):
        return cls(**data)

当比较房屋时,结果是它们不相等,因为在房间列表中的不同。 当比较它们在列表中时,房间是不同的。

自己 27个房间

[<housing.classes.Room object at 0x7f465f6f83c8>, <housing.classes.Room object at 0x7f465f6f8710>, <housing.classes.Room object at 0x7f465f6f8550>, <housing.classes.Room object at 0x7f465f6f8978>, <housing.classes.Room object at 0x7f465f6f8048>, <housing.classes.Room object at 0x7f465f6f8a20>, <housing.classes.Room object at 0x7f465f6f8518>, <housing.classes.Room object at 0x7f465f6f8898>, <housing.classes.Room object at 0x7f465f6f87b8>, <housing.classes.Room object at 0x7f465f6f84a8>, <housing.classes.Room object at 0x7f465f6f8278>, <housing.classes.Room object at 0x7f465f6f8c50>, <housing.classes.Room object at 0x7f465f6f8940>, <housing.classes.Room object at 0x7f465f6f86a0>, <housing.classes.Room object at 0x7f465f6f8320>, <housing.classes.Room object at 0x7f465f6f8c18>, <housing.classes.Room object at 0x7f465f6f8a58>, <housing.classes.Room object at 0x7f465f6f80b8>, <housing.classes.Room object at 0x7f465f6f8828>, <housing.classes.Room object at 0x7f465f6f8b70>, <housing.classes.Room object at 0x7f465f6f85f8>, <housing.classes.Room object at 0x7f465f6f81d0>, <housing.classes.Room object at 0x7f465f6f8358>, <housing.classes.Room object at 0x7f465f6f8240>, <housing.classes.Room object at 0x7f465f6f8128>, <housing.classes.Room object at 0x7f465f6f8400>, <housing.classes.Room object at 0x7f465f6f8668>]

其他27间

[<housing.classes.Room object at 0x7f465f6f1470>, <housing.classes.Room object at 0x7f465f6f1978>, <housing.classes.Room object at 0x7f465f6f1f98>, <housing.classes.Room object at 0x7f465f6f1d68>, <housing.classes.Room object at 0x7f465f6f1a58>, <housing.classes.Room object at 0x7f465f6f1cf8>, <housing.classes.Room object at 0x7f465f6f11d0>, <housing.classes.Room object at 0x7f465f6f1438>, <housing.classes.Room object at 0x7f465f6f1898>, <housing.classes.Room object at 0x7f465f6f1ef0>, <housing.classes.Room object at 0x7f465f6f10f0>, <housing.classes.Room object at 0x7f465f6f1198>, <housing.classes.Room object at 0x7f465f6f1240>, <housing.classes.Room object at 0x7f465f6f18d0>, <housing.classes.Room object at 0x7f465f6f17f0>, <housing.classes.Room object at 0x7f465f6f15c0>, <housing.classes.Room object at 0x7f465f6f1828>, <housing.classes.Room object at 0x7f465f6f1518>, <housing.classes.Room object at 0x7f465f6f13c8>, <housing.classes.Room object at 0x7f465f6f1f28>, <housing.classes.Room object at 0x7f465f6f1160>, <housing.classes.Room object at 0x7f465f6f1860>, <housing.classes.Room object at 0x7f465f6f12e8>, <housing.classes.Room object at 0x7f465f6f17b8>, <housing.classes.Room object at 0x7f465f6f1ba8>, <housing.classes.Room object at 0x7f465f6f1400>, <housing.classes.Room object at 0x7f465f6f14a8>]

但当比较比较每个房间列表中的房间时,我一直得到,所有的房间在 self 存在于 other.

作为工作的一部分,我添加了 House__eq__ 一个讨厌的检查,是这样的。

    def __eq__(self, other):
        '''
        Two houses instances will be equal when at least following values match:
         door,
         address
         rooms
        '''
        if not isinstance(other, House):
            return NotImplemented
        rooms_from_other = True
        rooms_from_self = True
        for room in self.rooms:
            if room not in other.rooms:
                rooms_from_self = False
                break
        for room in other.rooms:
            if room not in self.rooms:
                rooms_from_other = False
                break
        return (self.door == other.door and
                self.address == other.address and
                rooms_from_other and rooms_from_self)

有什么好的方法可以做到这一点? 为什么我的房间对象在直接比较时是平等的,而在列表中却不是?

谢谢。

python-3.x object hash compare
1个回答
1
投票

很可能两个房间列表的顺序不一样?(你的代码没有显示你如何建立它们)。

你可以先对这两个列表进行排序,然后比较排序后的列表。

return (self.door == other.door and
        self.address == other.address and
        sorted(self.rooms) == sorted(other.rooms))
© www.soinside.com 2019 - 2024. All rights reserved.