问题:Python列表可以有多大?

在Python中,列表可以有多大?我需要大约12000个元素的列表。我仍然可以运行列表方法(例如排序等)吗?

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


回答 0

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

PY_SSIZE_T_MAXpyport.h中定义为((size_t) -1)>>1

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

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

只要您拥有的元素数量等于或小于此数量,所有列表函数都应正确运行。

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

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

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

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

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


回答 1

Python文档所述

sys.maxsize

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

在我的计算机(Linux x86_64)中:

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

As the Python documentation says:

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.

In my computer (Linux x86_64):

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

回答 2

当然可以。实际上,您可以轻松地自己看到:

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

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

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

但是可以肯定,正如其他人所说。数组越大,操作将越慢。

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.


回答 3

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

此外,尽管列表很大,但列表方法/函数仍应继续工作。

如果您关心性能,那么值得研究一下NumPy之类的库。

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.

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.


回答 4

清单的性能特征在Effbot 进行了描述。

Python列表实际上是作为用于快速随机访问的向量实现的,因此容器基本上将容纳与内存中的空间一样多的项目。(您需要用于列表中包含的指针的空间以及在内存中用于指向的对象的空间。)

追加是O(1)(摊销的恒定复杂度),但是,插入/从序列中间删除将需要O(n)(线性复杂度)重新排序,这将随着列表中元素数量的增加而变慢。

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

反转只需要交换列表中所有指针所需的时间O(n)(由于触摸每个指针一次,所以有必要(线性复杂度))。

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.)

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.

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.

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).


回答 5

12000个元素在Python中什么都没有…实际上,只要Python解释器在您的系统上具有内存,元素的数量就可以增加。

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.


回答 6

对于不同的系统,它会有所不同(取决于RAM)。最简单的找出方法是

import six six.MAXSIZE 9223372036854775807 这使的最大尺寸listdict太,按照该文件

It varies for different systems (depends on RAM). The easiest way to find out is

import six six.MAXSIZE 9223372036854775807 This gives the max size of list and dict too ,as per the documentation


回答 7

我想说,您仅受可用RAM总量的限制。显然,数组越大,对其进行的操作就越长。

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.


回答 8

我是在x64位系统上从这里获得的:win32上的Python 3.7.0b5(v3.7.0b5:abb8802389,2018年5月31日,01:54:01)[MSC v.1913 64位(AMD64)]

在此处输入图片说明

I got this from here on a x64 bit system: Python 3.7.0b5 (v3.7.0b5:abb8802389, May 31 2018, 01:54:01) [MSC v.1913 64 bit (AMD64)] on win32

enter image description here


回答 9

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

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


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。