开发者

Python: function returning numbers in ascending order

开发者 https://www.devze.com 2023-02-20 17:30 出处:网络
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 ret

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

精彩评论

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

关注公众号