比如[1,2,4,6],最大6,最小1,返回6-1=5我寫的代碼:def checkio(*args):if not args:return 0return max(args)-min(args)另外兩個寫得更簡潔的代碼:版本A:def checkio(*args):return max(args) -min(args) if args else 0版本B:def checkio(*t):return len(t) and max(t)-min(t)版本A中if else為什么不用分號?版本B我不明白為什么這么寫能實現和我一樣的功能。len(t)是一個數值,max(t)-min(t)是一個數值,兩個數值and一下,怎么就能實現這個功能了呢?謝謝!當輸入為空的list的時候返回0。
1 回答
catspeake
TA貢獻1111條經驗 獲得超0個贊
A :if ...: pass else: pass 這是正規的python if else 語句 用啥分號? B 應該這樣子寫才對吧 def checkio(args): return max(args) -min(args) if args else 0等價于 def checkio(args): if args : return max(args) -min(args) else: return 0 C def checkio(t): return len(t) and max(t)-min(t) 等價于 def checkio(t): if len(t)==0: return len(t) else: return max(t)-min(t) |
添加回答
舉報
0/150
提交
取消
