謝謝諸先進提出這許多指導。經過提醒,才發現原來的推導,漏洞其實還蠻多的。而後來這許多程式輸入的討論還加入許多其他符號,其實,如果平常沒有接觸或是工作領域不相關看起來還真是看不太懂。有關後來的推導部份倒是讓小弟也有所了解謝謝您。
#get max from reverse array sortingnumbers = [1, 3, 4, 2, 5]numbers.sort(reverse=True)print(numbers[0])#get max using built-in function, max()numbers2 = [1, 3, 4, 2, 5]print(max(numbers2))
禮誠工作室 wrote:python 對我最大的麻煩,就是沒有 switch 的命令(雖然有第三方寫出套用工具,但是還是不好用), 變成要用一堆的 if else, else if ...... 來組合! 因為switch在python中是 dictionary+lambda的subset,搭配decorator或是inheritance可以產生出很多syntax sugar。
bernie_w39 wrote:if a > b and a > c: print('a 最大')if b > a and b > c: print('b 最大')if c > a and c > b: print('c 最大') Python支援比較運算子的串接,因此a > b and a > c可簡化為b < a> c(在執行上a的數值只要載入一次也比較有效率)。另外,由於各條件互不可能同時成立,改用elif會較有效率:if b < a> c: print('a 最大')elif a < b> c: print('b 最大')elif a < c > b: print('c 最大')