What are the memory requirements for a large Python list?

I did stupid things like:

from itertools import *
rows = combinations(range(0, 1140), 17)
all_rows = []
for row in rows:
    all_rows.append(row)

Not surprising; I have run out of memory address space (32-bit python 3.1) My question is: how to calculate how much memory address space I need for a large list? In this case, the list is in order 2.3X10^37. Is there a function in Python that returns the information I'm looking for, or the size of a smaller but similar list? What are these tools?

+5
source share
4 answers

There is a convenient function sys.getsizeof()(starting with Python 2.6) that helps with this:

>>> import sys
>>> sys.getsizeof(1)  # integer
12
>>> sys.getsizeof([]) # empty list
36
>>> sys.getsizeof(()) # empty tuple
28
>>> sys.getsizeof((1,))  # tuple with one element
32

, 12 , 4 ( 32- ) (36 28 ).

17 , 17*(12+4)+28 300 . - , 36 4 . , ( N), 36+N*(4+300) .

: , . Python , , ( [-5, 256] Python 2.6.4 Windows) . 257, . ( Python 257 is not 257+0; -)).

+11

, :

all_rows = []
for row in rows:
    all_rows.append(row)

:

all_rows = list(rows)

.

:

  • , ; , , , , .
  • ; , 4 32- 8 64- ; , (4 8 ) ( , Python)

, Python sys.getsizeof() :

>>> import sys
>>> sys.getsizeof([None] * 100)
872
+4

: --- array - :

[array] , : , , . , , , , . [...].

+3

All Articles