MemoryError when list append... plz help
bockman at virgilio.it
Wed Dec 31 09:31:57 CET 2008
[BON] ha scritto:
> for i in range(11000-1):
> for j in range(i+1, 11000):
> above sim is floating type.
> s.append is totally coducted 60,494,500 times.
> but this code raise MemoryError.
> My computer has 4G RAM.
> i think it's enough. but it doesn't...
> So, i've tested below code.
> while i<60494500 :
> but this code raise also MemoryError.
> How can i resolve this problem?
> please, help...
If you _really_ have to store so many numbers in memory (hint: if you
are processing them sequentially, you don't need to store all them - use
generators instead) then you may have better luck using mmap module to
create a huge file-memory object, that you can access both as a file and
as a list, and put numbers in it after packing/unpacking with struct.
Something like this (only marginally tested ):
>>> memory = mmap.mmap(-1, 60494500*4)
>>> def memory_put(offset, f):
... memory[offset*4:(offset+1)*4] = struct.pack( "%f", f )
>>> def memory_get(offset):
... return struct.unpack( "f", memory[offset*4:(offset+1)*4] )
>>> memory_put(12, 3.14 )
More information about the Python-list