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)
精彩评论