使用比较函数查找两个对象列表的交集
Find the intersection of two lists of objects by using a comparison function
tl;博士:
我在 Python 中有两个 class 对象列表(都是同一类型),在比较它们时,我需要通过使用returns True
或 False
.
的函数
我需要比较汽车的轮胎以确定轮胎是否匹配(“它会适合”),有时轮胎是否相同(相同brand/make/year)。
Given the example below, how can I compare the car's current tires to a list of potential tires I got back from the previous find
call using the compare.is_exact_match
function?
我见过类似的问题,例如 ,它让我可以根据 属性 的值比较对象,但我不确定这种方法是否(如何)适用于功能比较。
详情:
我有一个Car
,它的一个属性是一组轮胎(subclass Tire
):
def Car:
def __init__(self, ...args):
self.make = make
self.model = model
self.year = year
self.trim = trim
self.color = color
self.vin = vin
self.tires: [Tire] = tires
self.potential_matches: [Tire] = []
def Tire:
def __init__(self, ...args):
self.brand = brand
self.model_number = model_number
self.year = year
self.diameter = diameter
self.width = width
self.sidewall = sidewall
self.circumference = self.circumference
self.revs_per_mile = self.revs_per_mile
我有一些比较复杂的启发式方法,可以比较汽车列表以找到与这辆车匹配的轮胎。这是一个模拟示例,但我们假设这是一个轮胎交换市场,以便您了解我们如何去那里。在这种理论产品中,市场上的轮胎数据往往是不完整的。确定轮胎是否匹配非常昂贵,因此我们做了一些准备工作以减少足迹。一旦我们有了它,我们通常需要进一步比较轮胎以区分“只是匹配”和“相同的轮胎”。
这看起来像:
@classmethod
def find(cls, car: Car, marketplace_cars: [Car]) -> [Car.Tire]:
matches = list(filter(lambda c:
# One thing we can try is comparing the car first. If the car is a match,
# the tires probably are too.
car.make == c.make
and car.model == c.model
and car.year == c.year
and car.trim == c.trim
# If any of those fail, run the expensive heuristic
and compare.is_match(car, c)
marketplace_cars))
# Extract a list of tires from the list of cars that were identified as having matches
matching_tires = list(itertools.chain(*[m.tires for m in matches]))
return matching_tires
现在,我们希望从此列表中进一步缩小范围,以显示所有 相同 匹配的轮胎。这意味着在第一次比较中比较品牌、型号和里程以及所有其他内容。这次 compare.is_exact_match
获取单个 Tire
对象并比较它们。
这就是我迷路的地方:
How can I compare the car's current tires to a list of potential tires I got back from the previous find
call using the compare.is_exact_match
function?
@classmethod
def find_exact(cls, car: Car) -> [Car.Tire]:
# Pseudo code, because what do I do here?
# Should/can I do this with sets, or?
exact_matches = list(filter(
lambda tire, match: compare.is_exact_match(tire, match),
(car.tires, car.potential_matches)))
# Returns an array of [Car.Tire] that are exact matches
return exact_matches
事实证明我真的想多了,我只需要一个简单的列表理解。
@classmethod
def find_exact(cls, car: Car) -> [Car.Tire]:
exact_matches = [m for t in car.tires for m in car.potential_matches if compare.is_exact_match(t, m)]
"""
for t in car.tires:
for m in car.potential_matches:
if compare.is_exact_match(t, m):
m
"""
# Returns an array of [Car.Tire] that are exact matches
return exact_matches
tl;博士:
我在 Python 中有两个 class 对象列表(都是同一类型),在比较它们时,我需要通过使用returns True
或 False
.
我需要比较汽车的轮胎以确定轮胎是否匹配(“它会适合”),有时轮胎是否相同(相同brand/make/year)。
Given the example below, how can I compare the car's current tires to a list of potential tires I got back from the previous
find
call using thecompare.is_exact_match
function?
我见过类似的问题,例如
详情:
我有一个Car
,它的一个属性是一组轮胎(subclass Tire
):
def Car:
def __init__(self, ...args):
self.make = make
self.model = model
self.year = year
self.trim = trim
self.color = color
self.vin = vin
self.tires: [Tire] = tires
self.potential_matches: [Tire] = []
def Tire:
def __init__(self, ...args):
self.brand = brand
self.model_number = model_number
self.year = year
self.diameter = diameter
self.width = width
self.sidewall = sidewall
self.circumference = self.circumference
self.revs_per_mile = self.revs_per_mile
我有一些比较复杂的启发式方法,可以比较汽车列表以找到与这辆车匹配的轮胎。这是一个模拟示例,但我们假设这是一个轮胎交换市场,以便您了解我们如何去那里。在这种理论产品中,市场上的轮胎数据往往是不完整的。确定轮胎是否匹配非常昂贵,因此我们做了一些准备工作以减少足迹。一旦我们有了它,我们通常需要进一步比较轮胎以区分“只是匹配”和“相同的轮胎”。
这看起来像:
@classmethod
def find(cls, car: Car, marketplace_cars: [Car]) -> [Car.Tire]:
matches = list(filter(lambda c:
# One thing we can try is comparing the car first. If the car is a match,
# the tires probably are too.
car.make == c.make
and car.model == c.model
and car.year == c.year
and car.trim == c.trim
# If any of those fail, run the expensive heuristic
and compare.is_match(car, c)
marketplace_cars))
# Extract a list of tires from the list of cars that were identified as having matches
matching_tires = list(itertools.chain(*[m.tires for m in matches]))
return matching_tires
现在,我们希望从此列表中进一步缩小范围,以显示所有 相同 匹配的轮胎。这意味着在第一次比较中比较品牌、型号和里程以及所有其他内容。这次 compare.is_exact_match
获取单个 Tire
对象并比较它们。
这就是我迷路的地方:
How can I compare the car's current tires to a list of potential tires I got back from the previous
find
call using thecompare.is_exact_match
function?
@classmethod
def find_exact(cls, car: Car) -> [Car.Tire]:
# Pseudo code, because what do I do here?
# Should/can I do this with sets, or?
exact_matches = list(filter(
lambda tire, match: compare.is_exact_match(tire, match),
(car.tires, car.potential_matches)))
# Returns an array of [Car.Tire] that are exact matches
return exact_matches
事实证明我真的想多了,我只需要一个简单的列表理解。
@classmethod
def find_exact(cls, car: Car) -> [Car.Tire]:
exact_matches = [m for t in car.tires for m in car.potential_matches if compare.is_exact_match(t, m)]
"""
for t in car.tires:
for m in car.potential_matches:
if compare.is_exact_match(t, m):
m
"""
# Returns an array of [Car.Tire] that are exact matches
return exact_matches