對映函式(Mapping functions)
R 有一組 *apply 家族函式,可以將某個函式套用到 list/data.frame/matrix 的多個或全部元素。
在 Python 中,對應的是內建的 map() 函式。map() 的第一個引數是要套用的函式名稱,
第二個引數是一組值(list)。
指定的函式會逐一套用到第二個引數裡的所有值。請記得,你需要把 map() 的輸出包在 list() 裡,才能得到想要的結果。
在本章的[第三個練習](https://campus.datacamp.com/courses/python-for-r-users/control-flow-loops-and-functions?ex=3) 中,你用 for 迴圈來判定 num_drinks 中每個元素的暴飲狀態。這題你將改用 map(),搭配你在[第五題練習](https://campus.datacamp.com/courses/python-for-r-users/control-flow-loops-and-functions?ex=5) 中定義的函式,達成相同的效果。
本練習屬於課程
給 R 使用者的 Python
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# map the binge_male function to num_drinks
print(list(map(____, ____)))