By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,133 Members | 1,092 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,133 IT Pros & Developers. It's quick & easy.

1-liner to iterate over infinite sequence of integers?

P: n/a
I can do this with a generator:

def integers():
x = 1
while (True):
yield x
x += 1

for i in integers():

Is there a more elegant/concise way?

Oct 13 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Neal Becker wrote:
I can do this with a generator:

def integers():
x = 1
while (True):
yield x
x += 1

for i in integers():

Is there a more elegant/concise way?


import itertools
for i in itertools.count():
print i
Will McGugan
--
http://www.willmcgugan.com
"".join({'*':'@','^':'.'}.get(c,0) or chr(97+(ord(c)-84)%26) for c in
"jvyy*jvyyzpthtna^pbz")
Oct 13 '05 #2

P: n/a
Will McGugan wrote:
Neal Becker wrote:
I can do this with a generator:

def integers():
x = 1
while (True):
yield x
x += 1

for i in integers():
Is there a more elegant/concise way?


import itertools
for i in itertools.count():
print i


Actualy itertools.count(1) to start at 1..

Will McGugan
--
http://www.willmcgugan.com
"".join({'*':'@','^':'.'}.get(c,0) or chr(97+(ord(c)-84)%26) for c in
"jvyy*jvyyzpthtna^pbz")
Oct 13 '05 #3

P: n/a
On Thu, 13 Oct 2005 13:31:13 -0400, Neal Becker wrote:
I can do this with a generator:

def integers():
x = 1
while (True):
yield x
x += 1

for i in integers():

Is there a more elegant/concise way?


Others have given answers involving xrange() and itertools.count(), but I
thought I'd just mention that in my opinion, what you have written is
pretty elegant and concise and best of all, doesn't have the same problems
xrange() and itertools.count() have when then hit maxint. Not everything
needs to be a one-liner or a mysterious blackbox.

You could even modify your function to take start and step arguments:

def integers(start=1, step=1):
x = start
while True:
yield x
x += step

for odd in integers(step=2):
print odd

for even in integers(0, 2):
print even

etc.
--
Steven.

Oct 13 '05 #4

P: n/a
Steven D'Aprano <st***@REMOVETHIScyber.com.au> writes:
Others have given answers involving xrange() and itertools.count(), but I
thought I'd just mention that in my opinion, what you have written is
pretty elegant and concise and best of all, doesn't have the same problems
xrange() and itertools.count() have when then hit maxint. Not everything
needs to be a one-liner or a mysterious blackbox.


When you say "problems" with xrange or itertools.count hitting maxint,
the correct word is "bugs". The right thing to do with library bugs
is fix them, not clutter up your application code to circumvent them.
xrange, at least, raises an error if you give it too large an
argument, but what itertools.count does is just plain dangerous.

I just opened SF bug 1326277 about itertools.count.
Oct 13 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.