How to get the n next values of a generator in a list (python)

后端 未结 5 1439
醉话见心
醉话见心 2020-11-28 11:08

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():
           


        
5条回答
  •  再見小時候
    2020-11-28 11:44

    Use cytoolz.take.

    >>> from cytoolz import take
    >>> list(take(2, [10, 20, 30, 40, 50]))
    [10, 20]
    

提交回复
热议问题