有没有办法将以下内容简化为两行代码,而不是四行?
# convert to minutes (int)
if isinstance(r1, datetime.time):
r1 = r1.minute + (r1.hour * 60)
if isinstance(r2, datetime.time):
r2 = r2.minute + (r2.hour * 60)
答案 0 :(得分:2)
不确定
def accumulateMinutesIfDateTime(r):
if not isinstance(r, datetime.time):
return r
return r.minute + r.hour * 60
# ...
r1 = accumulateMinutesIfDateTime(r1)
r2 = accumulateMinutesIfDateTime(r2)
答案 1 :(得分:0)
你可以缩短它,但可能不那么容易理解:
r1, r2 = [r.minute + (r.hour * 60) if isinstance(r, datetime.time) else r for r in (r1, r2)]
答案 2 :(得分:0)
func = lambda r: r.minute + (r.hour * 60) if isinstance(r, datetime.time) else r
r1, r2 = func(r1), func(r2)