Python for循环生成列表

我就是我 2023-06-10 15:23 178阅读 0赞

1、示例

  1. list = [x*x for x in range(10)]
  2. print(list)

输出

  1. [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

在这里插入图片描述

2、小结

机器学习中常见这种列表生成方式

  1. featList = [fun(e) for e in dataSet]

说明,通过元素e循环取数据集dataSet,对e进行二次加工fun(e),依次放入列表featList。

3、应用

加载停用词表

  1. stop_word_path = './stopword.txt'
  2. stopword_list = [sw.replace('\n', '') for sw in open(stop_word_path,encoding='utf-8').readlines()]
  3. print(stopword_list)

输出
在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,178人围观)

还没有评论,来说两句吧...

相关阅读