Get the nth item of a generator in Python

前端 未结 7 2251
一向
一向 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:33

    Perhaps you should elaborate more on a actual use case.

    >>> gen = xrange(10)
    >>> ind=5 
    >>> gen[ind]
    5
    

提交回复
热议问题