这是我拥有的相关代码。它使用生成器从文件中获取单词。但是,在输入函数之前,单词首先存储到变量中。这个对吗?
这是否利用了生成器功能?
def do_something(words):
new_list = {}
for word in words:
// do stuff to each word
// then add to new_list
return new_list
def generate_words(input_file):
for line in input_file:
for word in line.split(' '):
// do stuff to word
yield word
if __name__ == '__main__':
with open("in.txt") as input_file:
words = generate_words(input_file)
do_something(words)
谢谢