I have a table (list of lists) in python like: (Python 3)
packets = [[a, b, (0+a), (0+b)], [e, f, (0+a+e), (0+b+f)], [i, j, (0+a+e+i), (0+b+f+j)]]
The first column is the size of packets and the second column is time difference. I want to calculate the total size in third column and sum of time in fourth column.
My code was :
for c in range (1, len(packets)):
packets[c][2] = packets[c-1][2] + packets[c][0]
packets[c][3] = packets[c-1][3] + packets[c][1]
but I got memoryerror
as the size of packets
is near a million.
Please suggest me a way to fix this the memory problem.
Try using the xrange function instead of range:
for c in xrange(1,len(packets)):
packets[c][2]=packets[c-1][2]+packets[c][0]
packets[c][3]=packets[c-1][3]+packets[c][1]
https://docs.python.org/2/library/functions.html#xrange
Edit
If you can't use xrange
, then make your own:
def myrange(start, end):
while True:
if start == end:
break
yield start
start += 1
for i in myrange(1, 10):
print i