开发者

Python: function returning numbers in ascending order

How can I write a function that would give me a new number in ascending order every time i call it?

For example, if I call it for the first time it returns "1", if I call again, i开发者_Go百科t returns "2".


How about itertools.count()?

counter = itertools.count()
print next(counter)
print next(counter)
print next(counter)

prints

0
1
2

If you want your counter to start from 1, use

counter = itertools.count(1)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜