今天提了个需求 说有一个商品列表 需要提取中元素中金额的最大值
废话不多说直接上码
# 假如现在有个商品列表
class ProductSeatMock(object):
def __init__(self, obj):
self.amt_receivable = obj["amt_receivable"]
self.seat = obj["seat"]
self.is_run_other_pro = obj["is_run_other_pro"]
# 我们老大的方法
def getMaxAmtReceivable(plist):
tmpPlist = [i for i in plist if i.seat is False and i.is_run_other_pro is True]
return max(tmpPlist, key=lambda x: x.amt_receivable) if len(tmpPlist) > 0 else None
# 我修改我们老大的方法
def getMax(plist):
tmpPlist= [i for i in plist if i.is_run_other_pro]
return max(tmpPlist, key=lambda x: x.amt_receivable) if tmpPlist else None
p1 = ProductSeatMock({"amt_receivable": 5, "seat": False, "is_run_other_pro": True})
p2 = ProductSeatMock({"amt_receivable": 6, "seat": True, "is_run_other_pro": True})
p3 = ProductSeatMock({"amt_receivable": 7, "seat": True, "is_run_other_pro": True})
p4 = ProductSeatMock({"amt_receivable": 8, "seat": False, "is_run_other_pro": True})
p5 = ProductSeatMock({"amt_receivable": 9, "seat": False, "is_run_other_pro": True})
p6 = ProductSeatMock({"amt_receivable": 1, "seat": True, "is_run_other_pro": True})
p7 = ProductSeatMock({"amt_receivable": 2, "seat": True, "is_run_other_pro": True})
plist = [p1, p2, p3, p4, p5, p6, p7]
result = getMax(plist)
print(result)