简体   繁体   English

如何从itertools计数对象打印?

[英]How to print from itertools count object?

I'm using itertools count method to keep track of how many instances of a class have been created. 我正在使用itertools count方法来跟踪已创建一个类的实例的数量。 My simplified code looks something like this: 我的简化代码如下所示:

from itertools import count
a = count(1)
a.next()
a.next()
print a

count(3) 算(3)

I want to print just the "3", without count. 我只想打印“ 3”,不计数量。 Sounds simple right? 听起来很简单吧?

if it sounds simple ... it probably is 如果听起来简单...可能是

from itertools import count
a = count(1)

next(a)
next(a)
print next(a)

you can also use itertools.islice to skip parts of an iterator 您还可以使用itertools.islice跳过迭代器的一部分

from itertools import count,islice
a = count(1)
for item in islice(a,2,4):
    print item

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM