Reputation: 85
I have been using python some months and this problem just appear two or three days ago. At this moment I am running this code in the IDLE 3.4.3 python GUI :
x = [1,2,3,4,5]
for i in x:
x.append((i * (i + 1))/2)
print(x)
But this hasnt output or error(is not the only one), the console just is opened and its waiting(like thinking) and then i check the process in task admin and i see that the process start in 30-35 mb and after One or Two minutes the process is consuming:
I have not installed new software or something with the OS, this happen in two different laptops(W7 and W10), could be this code, I know(its works if I create a new empty list) but what about other simple instructions like 1+1 I have tried with diferent IDEs, and python versions including architecture. First I was using iPython notebook and Spyder because I need to plot and Anaconda comes with everything ready, but the kernel always said Busy and without output; I restart, interrupt, new kernel but doesnt work and this happen just this week, because i was working perfectly so I had to remove it because this is happening.
Someone has idea what is happening?
Upvotes: 0
Views: 2072
Reputation: 427
To see what is happening, let me modify your code slightly
x = [1,2,3,4,5]
for i in x:
x.append((i * (i + 1))/2)
print(x)
if len(x) > 20:
break
print(x)
The output looks like this
[1, 2, 3, 4, 5, 1.0]
[1, 2, 3, 4, 5, 1.0, 3.0]
[1, 2, 3, 4, 5, 1.0, 3.0, 6.0]
[1, 2, 3, 4, 5, 1.0, 3.0, 6.0, 10.0]
[1, 2, 3, 4, 5, 1.0, 3.0, 6.0, 10.0, 15.0]
[1, 2, 3, 4, 5, 1.0, 3.0, 6.0, 10.0, 15.0, 1.0]
[1, 2, 3, 4, 5, 1.0, 3.0, 6.0, 10.0, 15.0, 1.0, 6.0]
[1, 2, 3, 4, 5, 1.0, 3.0, 6.0, 10.0, 15.0, 1.0, 6.0, 21.0]
[1, 2, 3, 4, 5, 1.0, 3.0, 6.0, 10.0, 15.0, 1.0, 6.0, 21.0, 55.0]
[1, 2, 3, 4, 5, 1.0, 3.0, 6.0, 10.0, 15.0, 1.0, 6.0, 21.0, 55.0, 120.0]
[1, 2, 3, 4, 5, 1.0, 3.0, 6.0, 10.0, 15.0, 1.0, 6.0, 21.0, 55.0, 120.0, 1.0]
[1, 2, 3, 4, 5, 1.0, 3.0, 6.0, 10.0, 15.0, 1.0, 6.0, 21.0, 55.0, 120.0, 1.0, 21.0]
[1, 2, 3, 4, 5, 1.0, 3.0, 6.0, 10.0, 15.0, 1.0, 6.0, 21.0, 55.0, 120.0, 1.0, 21.0, 231.0]
[1, 2, 3, 4, 5, 1.0, 3.0, 6.0, 10.0, 15.0, 1.0, 6.0, 21.0, 55.0, 120.0, 1.0, 21.0, 231.0, 1540.0]
[1, 2, 3, 4, 5, 1.0, 3.0, 6.0, 10.0, 15.0, 1.0, 6.0, 21.0, 55.0, 120.0, 1.0, 21.0, 231.0, 1540.0, 7260.0]
[1, 2, 3, 4, 5, 1.0, 3.0, 6.0, 10.0, 15.0, 1.0, 6.0, 21.0, 55.0, 120.0, 1.0, 21.0, 231.0, 1540.0, 7260.0, 1.0]
append
simply adds a new value to the end of a list, so what you've done is created an infinite loop where x
is just going to grow bigger and bigger. Basically every time the iterator draws a number from x
a new number is added to the end of x
, so the iterator will always have 5 more numbers to draw before it completes.
As the iterator keeps running, x
keeps getting bigger and bigger and consuming more and more memory.
To fix this infinite loop you can either store the results in a different list or have a break
condition like I used above if you really want the results to be appended to x
. Also, if you are meaning to replace the values in x
you can do something like this
x = [1,2,3,4,5]
for i, v in enumerate(x):
x[i] = (v * (v + 1))/2
print(x)
which outputs
[1.0, 3.0, 6.0, 10.0, 15.0]
Hope that helps.
Upvotes: 3