如何获取列表中生成器的n个下一个值(Python) [英] How to get the n next values of a generator in a list (python)
本文介绍了如何获取列表中生成器的n个下一个值(Python)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我制作了一个生成器来逐字读取文件,并且效果很好.
I have made a generator to read a file word by word and it works nicely.
def word_reader(file):
for line in open(file):
for p in line.split():
yield p
reader = word_reader('txtfile')
next(reader)
获取列表中的n个下一个值的最简单方法是什么?
What is the easiest way of getting the n next values in a list?
推荐答案
使用itertools.islice
:
list(itertools.islice(it, n))
这篇关于如何获取列表中生成器的n个下一个值(Python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文