获取python的itertools循环当前元素 [英] Getting python's itertools cycle current element
本文介绍了获取python的itertools循环当前元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道你可以使用 c = cycle(['a', 'b', 'c'])
使用 c.next()
在元素之间循环>,但是是否可以获取迭代器的当前元素?
I know you can use c = cycle(['a', 'b', 'c'])
to cycle between the elements using c.next()
, but is there away to get the iterator's current element?
例如如果 c.next()
返回 'c'
,则表示迭代器之前位于 'b'
.有没有办法在不使用 next()
的情况下获得 'b'
?
for example if c.next()
returned 'c'
, it means that iterator was at 'b'
before. Is there a way I can get 'b'
without using next()
?
推荐答案
迭代器/生成器没有任何方法可以获取当前值.您应该保留对它的引用,或者创建一些为您保留它的包装器.
Iterators/generators don't have any way to get the current value. You should either keep a reference to it or create some wrapper that holds onto it for you.
这篇关于获取python的itertools循环当前元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文