Python Tutorial 第三堂(2)資料處理函式
|
Python Tutorial 第三堂(1)社群文件資源 << 前情 此文件已有新版,詳見〈Python 3 Tutorial 第四堂(1)資料處理函式〉。 在 Python 的
range、zip 與 enumerate要瞭解這些資料處理函式如何使用,最好的方式就是從練習開始 … 練習 6:使用 在 Python 中,如果想使用 0, Justin 1, caterpillar 2, openhome 先給個小提示,程式基本上可以長這樣: names = ['Justin', 'caterpillar', 'openhome']
for ______ in ______:
print '{0}, {1}'.format(______)
試著想想,如果使用分別使用 別忘了,你可以在 Lab 檔案的 solutions 目錄找到解答。 reduce
舉例來說,如果你想要知道 初學者會有點難懂 如果 這個 練習 7:使用 使用 def ascending(a, b): return a - b
def descending(a, b): return -ascending(a, b)
# selection sort
def sorted(xs, compare = ascending):
return [] if not xs else __select(xs, compare)
def __select(xs, compare):
selected = xs[0]
for elem in xs[1:]:
if compare(elem, selected) < 0:
selected = elem
remain = []
selected_list = []
for elem in xs:
if elem != selected:
remain.append(elem)
else:
selected_list.append(elem)
return xs if not remain else selected_list + __select(remain, compare)
print sorted([2, 1, 3, 6, 5])
print sorted([2, 1, 3, 6, 5], descending)
關於函數式程式設計想要知道 不過,何時該使用函數式的元素,取決於可讀與風格問題,在 Python 中確實是有那麼一些函數式程式設計的元素, 無論如何,藉由這個練習,瞭解到 Python 可進行多重典範設計,只要你願意的話,函數式是可以的設計之一;接下來的內容,是要介紹 Python 中對永續化(Persistence)的基本支援,順便來看看幾種永續設計方式。 |

Java 學習之路






劉鼎元
08/26練習7似乎有點囉唆XDD
其實每次就是把最小(或最大)的挑出來然後剩下再挑出來最小…