《機器學習實戰》學習筆記——第3章 決策樹

程序清單3-3中有一句

featList = [example[i] for example in dataSet]

乍一看覺得困惑,但是又大概知道它要做什麼,前幾天查資料的時候好像有碰到for的這個用法。
感覺上面那句是下面這句的簡寫
有人知道這種用法的更好的說明希望多多指教

for example in dataSet:
    featList.append(example[i])

測試:

>>> dataSet = [[1, 'a', 'yes'], [2, 'b', 'yes'], [3, 'c', 'no'], [4, 'd', 'no'], [5, 'e', 'no']]
>>> list = [example[1] for example in dataswet]
>>> list
Out[21]: ['a', 'b', 'c', 'd', 'e']
>>> myList = []
>>> for example in dataSet:
...     myList.append(example[1])
>>> myList
Out[30]: ['a', 'b', 'c', 'd', 'e']
發佈了34 篇原創文章 · 獲贊 10 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章