it would contain at most 1000 x 1000 x 1000 elements, which is too big for python dictionary.
with dict, around 30 x 1000 x 1000 elements, on my machine it already consumed 2gb of memory and everything got stoned.
any modules that can handle 3-dimension array whose value would be only True/False? I check bitarray http://pypi.python.org/pypi/bitarray, which seems reasonable and coded in C, however it seems more like a bit-stream instead of an array, since it supports only 1 dimension.
numpyhas already been suggested by EnricoGiampieri, and if you can use this, you should.Otherwise, there are two choices:
A jagged array, as suggested by NPE, would be a
listoflistofbitarrays. This allows you to have jagged bounds—e.g., each row could be a different width, or even independently resizable:Alternatively, as suggested by Xymostech, do your own indexing on a 1-D array:
Either way, you’d probably want to wrap this up in a class, so you can do this:
That’s as easy as:
Of course if you want more functionality (like slicing), you have to write a bit more.
The jagged array will use a bit more memory (after all, you have the overhead of 1M
bitarrayobjects and 1Klistobjects), and may be a bit slower, but this usually won’t make much difference.The important deciding factor should be whether it’s inherently an error for your data to have jagged rows. If so, use the second solution; if it might be useful to have jagged or resizable rows, use the former. (Keeping in mind that I’d use
numpyover either solution, if at all possible.)