程序清單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']