A list with endless emotions

I need to work with two separate infinite lists of numbers, but could not find a way to generate, store and manage it in python.

Can someone suggest me a way to deal with endless arithmetic processing or any series and how to work with them, given the fact of minimal use of memory and time.

Thank you all for your suggestions in advance.

+7
source share
8 answers

You are looking for a python generator :

def infinitenumbers(): count = 0 while True: yield count count += 1 

The itertools package comes with a predefined count generator.

 >>> import itertools >>> c = itertools.count() >>> next(c) 0 >>> next(c) 1 >>> for i in itertools.islice(c, 5): ... print i ... 2 3 4 5 6 
+17
source

Here is the iterator . You cannot have an infinite list of numbers, but you can have an infinite iterator.

 import itertools arithmetic_progression = itertools.count(start,step) #from the python docs 

Docs for Python2 can be found here.

+2
source

To create an object that acts as a β€œmutable” infinite list, you can overload the __getitem__ and __setitem__ methods in the class:

 class infinite_list(): def __init__(self, func): self.func = func self.assigned_items = {} def __getitem__(self, key): if key in self.assigned_items: return self.assigned_items[key] else: return self.func(key) def __setitem__(self, key , value): self.assigned_items[key] = value 

Then you can initialize the "infinite list" with a lambda expression and change the item in the list:

 infinite_thing = infinite_list(lambda a: a*2) print(infinite_thing[1]) #prints "2" infinite_thing[1] = infinite_thing[2] print(infinite_thing[1]) #prints "4" 

Similarly, you can create an β€œ infinite dictionary ” that provides a default value for each missing key.

+1
source

Perhaps the natural way to generate an infinite series is to use a generator:

 def arith(a, d): while True: yield a a += d 

This can be used like this:

 print list(itertools.islice(arith(10, 2), 100)) 
0
source

My decision:

 from hofs import * def cons_stream(head,tail): return [head,tail,False,False] def stream_cdr(strm): if strm[2]: return strm[3] strm[3] = strm[1]() strm[2] = True return strm[3] def show_stream(stream, num = 10): if empty(stream): return [] if num == 0: return [] return adjoin(stream[0], show_stream(stream_cdr(stream), num - 1)) def add_streams(a , b): if empty(a): return b if empty(b): return a return cons_stream(a[0] + b[0] , lambda : add_streams( stream_cdr(a), stream_cdr(b))) def stream_filter( pred , stream ): if empty(stream): return [] if pred(stream[0]): return cons_stream(stream[0], lambda : stream_filter(pred, stream_cdr(stream))) else: return stream_filter( pred , stream_cdr( stream )) def sieve(stream): return cons_stream(stream[0] , lambda : sieve(stream_filter(lambda x : x % stream[0] > 0 , stream_cdr(stream)))) ones = cons_stream(1, lambda : ones) integers = cons_stream(1, lambda : add_streams(ones, integers)) primes = sieve(stream_cdr(integers)) print(show_stream(primes)) 

Copy the above Python code. When I tried this, I got [2, 3, 5, 7, 11, 13, 17, 19, 23, 29], which is a 10 infinite list of primes.

0
source

You need hofs.py to be

 def empty(data): return data == [] def adjoin(value,data): result = [value] result.extend(data) return result def map(func, data): if empty(data): return [] else: return adjoin(func(data[0]), map(func, data[1:])) def keep(pred, data): if empty(data): return [] elif pred(data[0]): return adjoin( data[0] , keep(pred, data[1:])) else: return keep(pred, data[1:]) 
0
source

I have another python3 solution (read SICP chapter 3.5)

 class Stream: def __init__(self, head, tail): self.head = head self.tail = tail self.memory = None self.isDone = False def car(self): return self.head def cdr(self): if self.isDone: return self.memory self.memory = self.tail() self.isDone = True return self.memory def __getitem__(self, pullFrom): if pullFrom < 1 or self.memory == []: return [] return [self.car()] + self.cdr()[pullFrom - 1] def __repr__(self): return "[" + repr(self.car()) + " x " + repr(self.tail) + "]" def map(self, func): if self.memory == []: return [] return Stream(func(self.car()), lambda: Stream.map(self.cdr(), func)) def from_list(lst): if lst == []: return [] return Stream(lst[0], lambda: Stream.from_list(lst[1:])) def filter(self, pred): if self.memory == []: return [] elif pred(self.car()): return Stream(self.car(), lambda: Stream.filter(self.cdr(), pred)) else: return self.cdr().filter(pred) def sieve(self): return Stream(self.car(), lambda: self.cdr().filter(lambda n: n % self.car() > 0).sieve()) def foreach(self, action, pull = None): if pull is None: action(self.car()) self.cdr().foreach(action, pull) elif pull <= 0: return else: action(self.car()) self.cdr().foreach(action, pull-1)and run: a = Stream(0, lambda: a.map((lambda x: x + 1))) print(a[10]) 

which returns: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] .

But threads are lazily evaluated, therefore:

 >>> a = Stream(0, lambda: a.map((lambda x: x + 1))) >>> print(a) 

prints:

[0 x [...]]

0
source

I assume you want a list of infinite numbers in a range. I have a similar problem and here is my solution:

 c = 0 step = 0.0001 # the difference between the numbers limit = 100 # The upper limit myInfList = [] while c <= limit: myInfList.append(c) c = c + step print(myInfList) 
-1
source

All Articles