【发布时间】:2017-03-10 09:55:36
【问题描述】:
我正在尝试导入一个文本文件并将文本返回到每个单词的字符串列表中,同时还返回小写字母且没有标点符号。
我创建了以下代码,但这不会将每个单词拆分成一个字符串。也可以在理解中添加.lower() 吗?
def read_words(words_file):
"""Turns file into a list of strings, lower case, and no punctuation"""
return [word for line in open(words_file, 'r') for word in line.split(string.punctuation)]
【问题讨论】:
-
请添加示例输入、您想要作为输出获得的内容以及您获得的实际输出。
-
为什么流程需要是列表推导式?
-
这不需要是一种理解。只是认为这将是最少的代码
标签: python list list-comprehension