Infinite Series in Python [duplicate]










0
















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.










share|improve this 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















0
















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.










share|improve this 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













0












0








0









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.










share|improve this question















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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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

















  • 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












2 Answers
2






active

oldest

votes


















0














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)





share|improve this answer






























    0














    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)





    share|improve this answer





























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      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)





      share|improve this answer



























        0














        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)





        share|improve this answer

























          0












          0








          0







          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)





          share|improve this answer













          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)






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 15 '18 at 2:52









          Joran BeasleyJoran Beasley

          74k682121




          74k682121























              0














              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)





              share|improve this answer



























                0














                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)





                share|improve this answer

























                  0












                  0








                  0







                  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)





                  share|improve this answer













                  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)






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 15 '18 at 3:19









                  mhawkemhawke

                  59.7k85785




                  59.7k85785













                      Popular posts from this blog

                      Use pre created SQLite database for Android project in kotlin

                      Darth Vader #20

                      Ondo