Python MemoryError when appending a list -
i've small python (2.7.10) script, can see below.
def numbers_calc(max_num, num_step): """returns every number 0 max_num num_step step.""" n = 0 l = [] while n < max_num + 1: l.append(n) n += n * num_step return l n_l = [] n_l.append(numbers_calc(25, 1)) print "here numbers." print n_l
the function numbers_calc
meant take given args, form list, , populate numbers (with num_step
step when calculating) before reaches max_num + 1
. script return
it's local list named l
.
however, every time run script, encounter memoryerror
. here's python returned when ran script:
traceback (most recent call last): file "num.py", line 13, in <module> n_l.append(numbers_calc(25, 1)) file "ex33.py", line 7, in numbers_calc l.extend(i) memoryerror
i tried looking up, saw nothing helpful. hope can me!
the issue in line -
n += n * num_step
here, initialized n
0
, , multiplying n
, num_step
, result 0
, , adding n
. n
stays @ 0
. if try loop 0 max_num+1 every num_step, should use range()
function, example -
def numbers_calc(max_num, num_step): """returns every number 0 max_num num_step step.""" l = [] in range(0,max_num + 1,num_step): l.append(i) return l
Comments
Post a Comment