一个Python数组能有多大?

时间:2022-09-06 16:23:46

In Python, how big can an array/list get? I need an array of about 12000 elements. Will I still be able to run array/list methods such as sorting, etc?

在Python中,数组/列表有多大?我需要一个大约12000个元素的数组。我还能运行数组/列表方法,比如排序等吗?

8 个解决方案

#1


154  

According to the source code, the maximum size of a list is PY_SSIZE_T_MAX/sizeof(PyObject*).

根据源代码,列表的最大大小是PY_SSIZE_T_MAX/sizeof(PyObject*)。

PY_SSIZE_T_MAX is defined in pyport.h to be ((size_t) -1)>>1

PY_SSIZE_T_MAX在pyport中定义。h为((size_t) -1)>>1。

On a regular 32bit system, this is (4294967295 / 2) / 4 or 536870912.

在一个常规的32位系统上,这是(4294967295 / 2)/ 4或536870912。

Therefore the maximum size of a python list on a 32 bit system is 536,870,912 elements.

因此,在32位系统上的python列表的最大大小是536870,912个元素。

As long as the number of elements you have is equal or below this, all list functions should operate correctly.

只要您拥有的元素数量相等或低于此,所有列表函数都应该正确操作。

#2


36  

As the Python documentation says:

正如Python文档所述:

sys.maxsize

sys.maxsize

The largest positive integer supported by the platform’s Py_ssize_t type, and thus the maximum size lists, strings, dicts, and many other containers can have.

平台的Py_ssize_t类型支持的最大的正整数,因此最大大小列表、字符串、dicts和其他许多容器都可以使用。

In my computer (Linux x86_64):

在我的电脑(Linux x86_64):

>>> import sys
>>> print sys.maxsize
9223372036854775807

#3


24  

Sure it is OK. Actually you can see for yourself easily:

确定就可以了。实际上,你可以很容易地看到自己:

l = range(12000)
l = sorted(l, reverse=True)

Running the those lines on my machine took:

在我的机器上运行这些行:

real    0m0.036s
user    0m0.024s
sys  0m0.004s

But sure as everyone else said. The larger the array the slower the operations will be.

但正如其他人所说的那样。数组越大,操作就越慢。

#4


6  

In casual code I've created lists with millions of elements. I believe that Python's implementation of lists are only bound by the amount of memory on your system.

在临时代码中,我创建了包含数百万个元素的列表。我认为Python的列表的实现只受系统内的内存数量的限制。

In addition, the list methods / functions should continue to work despite the size of the list.

此外,列表方法/函数应该在列表的大小之外继续工作。

If you care about performance, it might be worthwhile to look into a library such as NumPy.

如果您关心性能,那么可以查看诸如NumPy之类的库。

#5


5  

Performance characteristics for lists are described on Effbot.

对列表的性能特征进行了描述。

Python lists are actually implemented as vector for fast random access, so the container will basically hold as many items as there is space for in memory. (You need space for pointers contained in the list as well as space in memory for the object(s) being pointed to.)

Python列表实际上是作为快速随机访问的矢量来实现的,所以容器基本上会保留许多项,因为内存中有空间。(您需要在列表中包含的指针的空间,以及指向被指向的对象的内存空间。)

Appending is O(1) (amortized constant complexity), however, inserting into/deleting from the middle of the sequence will require an O(n) (linear complexity) reordering, which will get slower as the number of elements in your list.

Appending是O(1)(平定常数复杂度),但是,从序列中间插入/删除需要一个O(n)(线性复杂度)重新排序,它会随着列表中元素的数量而变慢。

Your sorting question is more nuanced, since the comparison operation can take an unbounded amount of time. If you're performing really slow comparisons, it will take a long time, though it's no fault of Python's list data type.

您的排序问题更加微妙,因为比较操作可以花费无限的时间。如果您执行的比较慢,则需要很长时间,尽管这不是Python列表数据类型的错误。

Reversal just takes the amount of time it required to swap all the pointers in the list (necessarily O(n) (linear complexity), since you touch each pointer once).

反转只需要在列表中交换所有指针所需的时间(必须是O(n)(线性复杂度),因为您每次都要触摸每个指针)。

#6


4  

12000 elements is nothing in Python... and actually the number of elements can go as far as the Python interpreter has memory on your system.

在Python中,12000个元素是没有的…实际上,元素的数量可以达到Python解释器在系统上的内存。

#7


1  

I'd say you're only limited by the total amount of RAM available. Obviously the larger the array the longer operations on it will take.

我想说的是,你只受到可用RAM总量的限制。显然,数组越大,操作的时间就越长。

#8


-9  

There is no limitation of list number. The main reason which causes your error is the RAM. Please upgrade your memory size.

不存在列表号的限制。导致错误的主要原因是RAM。请升级您的内存大小。

#1


154  

According to the source code, the maximum size of a list is PY_SSIZE_T_MAX/sizeof(PyObject*).

根据源代码,列表的最大大小是PY_SSIZE_T_MAX/sizeof(PyObject*)。

PY_SSIZE_T_MAX is defined in pyport.h to be ((size_t) -1)>>1

PY_SSIZE_T_MAX在pyport中定义。h为((size_t) -1)>>1。

On a regular 32bit system, this is (4294967295 / 2) / 4 or 536870912.

在一个常规的32位系统上,这是(4294967295 / 2)/ 4或536870912。

Therefore the maximum size of a python list on a 32 bit system is 536,870,912 elements.

因此,在32位系统上的python列表的最大大小是536870,912个元素。

As long as the number of elements you have is equal or below this, all list functions should operate correctly.

只要您拥有的元素数量相等或低于此,所有列表函数都应该正确操作。

#2


36  

As the Python documentation says:

正如Python文档所述:

sys.maxsize

sys.maxsize

The largest positive integer supported by the platform’s Py_ssize_t type, and thus the maximum size lists, strings, dicts, and many other containers can have.

平台的Py_ssize_t类型支持的最大的正整数,因此最大大小列表、字符串、dicts和其他许多容器都可以使用。

In my computer (Linux x86_64):

在我的电脑(Linux x86_64):

>>> import sys
>>> print sys.maxsize
9223372036854775807

#3


24  

Sure it is OK. Actually you can see for yourself easily:

确定就可以了。实际上,你可以很容易地看到自己:

l = range(12000)
l = sorted(l, reverse=True)

Running the those lines on my machine took:

在我的机器上运行这些行:

real    0m0.036s
user    0m0.024s
sys  0m0.004s

But sure as everyone else said. The larger the array the slower the operations will be.

但正如其他人所说的那样。数组越大,操作就越慢。

#4


6  

In casual code I've created lists with millions of elements. I believe that Python's implementation of lists are only bound by the amount of memory on your system.

在临时代码中,我创建了包含数百万个元素的列表。我认为Python的列表的实现只受系统内的内存数量的限制。

In addition, the list methods / functions should continue to work despite the size of the list.

此外,列表方法/函数应该在列表的大小之外继续工作。

If you care about performance, it might be worthwhile to look into a library such as NumPy.

如果您关心性能,那么可以查看诸如NumPy之类的库。

#5


5  

Performance characteristics for lists are described on Effbot.

对列表的性能特征进行了描述。

Python lists are actually implemented as vector for fast random access, so the container will basically hold as many items as there is space for in memory. (You need space for pointers contained in the list as well as space in memory for the object(s) being pointed to.)

Python列表实际上是作为快速随机访问的矢量来实现的,所以容器基本上会保留许多项,因为内存中有空间。(您需要在列表中包含的指针的空间,以及指向被指向的对象的内存空间。)

Appending is O(1) (amortized constant complexity), however, inserting into/deleting from the middle of the sequence will require an O(n) (linear complexity) reordering, which will get slower as the number of elements in your list.

Appending是O(1)(平定常数复杂度),但是,从序列中间插入/删除需要一个O(n)(线性复杂度)重新排序,它会随着列表中元素的数量而变慢。

Your sorting question is more nuanced, since the comparison operation can take an unbounded amount of time. If you're performing really slow comparisons, it will take a long time, though it's no fault of Python's list data type.

您的排序问题更加微妙,因为比较操作可以花费无限的时间。如果您执行的比较慢,则需要很长时间,尽管这不是Python列表数据类型的错误。

Reversal just takes the amount of time it required to swap all the pointers in the list (necessarily O(n) (linear complexity), since you touch each pointer once).

反转只需要在列表中交换所有指针所需的时间(必须是O(n)(线性复杂度),因为您每次都要触摸每个指针)。

#6


4  

12000 elements is nothing in Python... and actually the number of elements can go as far as the Python interpreter has memory on your system.

在Python中,12000个元素是没有的…实际上,元素的数量可以达到Python解释器在系统上的内存。

#7


1  

I'd say you're only limited by the total amount of RAM available. Obviously the larger the array the longer operations on it will take.

我想说的是,你只受到可用RAM总量的限制。显然,数组越大,操作的时间就越长。

#8


-9  

There is no limitation of list number. The main reason which causes your error is the RAM. Please upgrade your memory size.

不存在列表号的限制。导致错误的主要原因是RAM。请升级您的内存大小。