《机器学习实战》学习笔记——第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万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章