1、示例
list = [x*x for x in range(10)]
print(list)
输出
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

2、小结
机器学习中常见这种列表生成方式
featList = [fun(e) for e in dataSet]
说明,通过元素e循环取数据集dataSet,对e进行二次加工fun(e),依次放入列表featList。
3、应用
加载停用词表
stop_word_path = './stopword.txt'
stopword_list = [sw.replace('n', '') for sw in open(stop_word_path,encoding='utf-8').readlines()]
print(stopword_list)
输出

最后
以上就是成就手套最近收集整理的关于Python for循环生成列表的全部内容,更多相关Python内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复