Infinite Series in Python [duplicate]
This question already has an answer here:
Is there an expression for an infinite generator?
7 answers
Is there any function in Python that provides an infinite series similar to generateSequence in Kotlin?
In Kotlin I can do something like:
generateSequence(1) it + 1 .take(5).forEach println(it)
Obviously this stops with an integer overflow error but I would like to do something similar in Python.
python kotlin
marked as duplicate by Community♦ Nov 15 '18 at 5:27
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Is there an expression for an infinite generator?
7 answers
Is there any function in Python that provides an infinite series similar to generateSequence in Kotlin?
In Kotlin I can do something like:
generateSequence(1) it + 1 .take(5).forEach println(it)
Obviously this stops with an integer overflow error but I would like to do something similar in Python.
python kotlin
marked as duplicate by Community♦ Nov 15 '18 at 5:27
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
This SO thread will be of interest to you: stackoverflow.com/questions/5737196/…
– Peter Leimbigler
Nov 15 '18 at 2:46
add a comment |
This question already has an answer here:
Is there an expression for an infinite generator?
7 answers
Is there any function in Python that provides an infinite series similar to generateSequence in Kotlin?
In Kotlin I can do something like:
generateSequence(1) it + 1 .take(5).forEach println(it)
Obviously this stops with an integer overflow error but I would like to do something similar in Python.
python kotlin
This question already has an answer here:
Is there an expression for an infinite generator?
7 answers
Is there any function in Python that provides an infinite series similar to generateSequence in Kotlin?
In Kotlin I can do something like:
generateSequence(1) it + 1 .take(5).forEach println(it)
Obviously this stops with an integer overflow error but I would like to do something similar in Python.
This question already has an answer here:
Is there an expression for an infinite generator?
7 answers
python kotlin
python kotlin
asked Nov 15 '18 at 2:38
Iroh4526Iroh4526
274
274
marked as duplicate by Community♦ Nov 15 '18 at 5:27
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Community♦ Nov 15 '18 at 5:27
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
This SO thread will be of interest to you: stackoverflow.com/questions/5737196/…
– Peter Leimbigler
Nov 15 '18 at 2:46
add a comment |
This SO thread will be of interest to you: stackoverflow.com/questions/5737196/…
– Peter Leimbigler
Nov 15 '18 at 2:46
This SO thread will be of interest to you: stackoverflow.com/questions/5737196/…
– Peter Leimbigler
Nov 15 '18 at 2:46
This SO thread will be of interest to you: stackoverflow.com/questions/5737196/…
– Peter Leimbigler
Nov 15 '18 at 2:46
add a comment |
2 Answers
2
active
oldest
votes
you can write a simple generator
def count(x):
while True:
yield x
x += 1
for i in count(5):
print(i)
of coarse this particular generator is builtin with itertools.count
import itertools
for i in itertools.count(5):
print(i)
add a comment |
Use itertools.count()
to get a count
object that generates an infinite sequence of values.
You can take the first n items by explicitly retrieving the next item from the count
object for the required number of times. Alternatively, and preferably, use itertools.islice()
to take the first n items.
Mirroring your example, to take the first 5 values of the sequence using explicit iteration:
from itertools import count
c = count(1) # start from 1 instead of 0
for i in range(5):
print(next(c))
Or using islice()
:
for n in islice(count(1), 5):
print(n)
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
you can write a simple generator
def count(x):
while True:
yield x
x += 1
for i in count(5):
print(i)
of coarse this particular generator is builtin with itertools.count
import itertools
for i in itertools.count(5):
print(i)
add a comment |
you can write a simple generator
def count(x):
while True:
yield x
x += 1
for i in count(5):
print(i)
of coarse this particular generator is builtin with itertools.count
import itertools
for i in itertools.count(5):
print(i)
add a comment |
you can write a simple generator
def count(x):
while True:
yield x
x += 1
for i in count(5):
print(i)
of coarse this particular generator is builtin with itertools.count
import itertools
for i in itertools.count(5):
print(i)
you can write a simple generator
def count(x):
while True:
yield x
x += 1
for i in count(5):
print(i)
of coarse this particular generator is builtin with itertools.count
import itertools
for i in itertools.count(5):
print(i)
answered Nov 15 '18 at 2:52
Joran BeasleyJoran Beasley
74k682121
74k682121
add a comment |
add a comment |
Use itertools.count()
to get a count
object that generates an infinite sequence of values.
You can take the first n items by explicitly retrieving the next item from the count
object for the required number of times. Alternatively, and preferably, use itertools.islice()
to take the first n items.
Mirroring your example, to take the first 5 values of the sequence using explicit iteration:
from itertools import count
c = count(1) # start from 1 instead of 0
for i in range(5):
print(next(c))
Or using islice()
:
for n in islice(count(1), 5):
print(n)
add a comment |
Use itertools.count()
to get a count
object that generates an infinite sequence of values.
You can take the first n items by explicitly retrieving the next item from the count
object for the required number of times. Alternatively, and preferably, use itertools.islice()
to take the first n items.
Mirroring your example, to take the first 5 values of the sequence using explicit iteration:
from itertools import count
c = count(1) # start from 1 instead of 0
for i in range(5):
print(next(c))
Or using islice()
:
for n in islice(count(1), 5):
print(n)
add a comment |
Use itertools.count()
to get a count
object that generates an infinite sequence of values.
You can take the first n items by explicitly retrieving the next item from the count
object for the required number of times. Alternatively, and preferably, use itertools.islice()
to take the first n items.
Mirroring your example, to take the first 5 values of the sequence using explicit iteration:
from itertools import count
c = count(1) # start from 1 instead of 0
for i in range(5):
print(next(c))
Or using islice()
:
for n in islice(count(1), 5):
print(n)
Use itertools.count()
to get a count
object that generates an infinite sequence of values.
You can take the first n items by explicitly retrieving the next item from the count
object for the required number of times. Alternatively, and preferably, use itertools.islice()
to take the first n items.
Mirroring your example, to take the first 5 values of the sequence using explicit iteration:
from itertools import count
c = count(1) # start from 1 instead of 0
for i in range(5):
print(next(c))
Or using islice()
:
for n in islice(count(1), 5):
print(n)
answered Nov 15 '18 at 3:19
mhawkemhawke
59.7k85785
59.7k85785
add a comment |
add a comment |
This SO thread will be of interest to you: stackoverflow.com/questions/5737196/…
– Peter Leimbigler
Nov 15 '18 at 2:46