pythonlambda内判定_python lambda和列表推导式判断列表中元素中指定值得max

今天提了个需求 说有一个商品列表 需要提取中元素中金额的最大值

废话不多说直接上码

# 假如现在有个商品列表

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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值