Get the nth item of a generator in Python

前端 未结 7 2218
一向
一向 2020-11-28 07:34

Is there a more syntactically concise way of writing the following?

gen = (i for i in xrange(10))
index = 5
for i, v in enumerate(gen):
    if i is index:
           


        
相关标签:
7条回答
  • 2020-11-28 08:36

    You could do this, using count as an example generator:

    from itertools import islice, count
    next(islice(count(), n, n+1))
    
    0 讨论(0)
提交回复
热议问题