python-3.x - 对象之间的比较匹配,但不是对象数组之间的比较
问题描述
所以我遇到的问题是在比较 Class 中的对象时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)
这样做的好方法是什么,为什么我的房间对象在直接比较但不在列表中时是相等的?
谢谢。
解决方案
很可能 2 个房间列表的排序方式不同?(您的代码没有显示您如何构建它们)。
您可以先对 2 个列表进行排序,然后他们比较排序后的列表。
return (self.door == other.door and
self.address == other.address and
sorted(self.rooms) == sorted(other.rooms))
推荐阅读
- c# - 串口打不开
- python - 在 Windows 10 中安装后无法加载“rpy2”模块
- sql - 在 SQL 查询结果中打开 Chrome
- java - 在 Azure DevOps 管道构建中收到此错误“无法执行目标 org.apache.maven.plugins:maven-compiler-plugin:3.5.1:compile”
- typescript - 获取联合数组的值
- javascript - 你能在 javascript 中添加脚本的来源吗
- python-3.x - Python:如何将日志记录添加到现有日志文件(以避免 .bak 文件)?
- c++ - 调用成员函数指针
- javascript - 使用 javascript 更新表单控件
- symfony - 如何为特定 Symfony 的路由创建 Nginx 的根?