python列表可存储的字节数有没有限制,是属于于链表还是栈的多寡结构?

发布日期:2018-06-06 来源:财富国际在线 阅读:
python列表可存储的字节数有没有限制,是属于于链表还是栈的多寡结构? 匿名用户 2小时前 124 python python列表 在python开发中,列表可以存储的最大字节数有没有限制?是不是属于链表的数据结构呢,如果是这个结构的话是否意味着它理论上可以存储无限大的数据?
0 0
其他回答
不知道你问的是list最大长度还是存储的元素的最大字节数。


如果是list的最大长度,是有上限的。

32位python的限制是 536870912 个元素64位python的限制是 1152921504606846975 个元素
热心网民 2小时前 0条评论
0 0
在 CPython里边 Python 的 list 是 PyObject* 的数组,如下:

typedef struct {    PyObject_VAR_HEAD    /* Vector of pointers to list elements.  list[0] is ob_item[0], etc. */    PyObject **ob_item;    /* ob_item contains space for 'allocated' elements.  The number     * currently in use is ob_size.     * Invariants:     *     0 <= ob_size <= allocated     *     len(list) == ob_size     *     ob_item == NULL implies ob_size == allocated == 0     * list.sort() temporarily sets allocated to -1 to detect mutations.     *     * Items must normally not be NULL, except during construction when     * the list is not yet visible outside the function that builds it.     */    Py_ssize_t allocated;} PyListObject;

ob_item是个数组,就是存值的地方。
PyObject *PyList_GetItem(PyObject *op, Py_ssize_t i){    if (!PyList_Check(op)) {        PyErr_BadInternalCall();        return NULL;    }    if (i < 0 || i >= Py_SIZE(op)) {        if (indexerr == NULL) {            indexerr = PyUnicode_FromString(                "list index out of range");            if (indexerr == NULL)                return NULL;        }        PyErr_SetObject(PyExc_IndexError, indexerr);        return NULL;    }    return ((PyListObject *)op) -> ob_item[i];}
(最后一行)并且取元素的时候就是这么用的。

有上限么?理论上来说有的…:

static intlist_resize(PyListObject *self, Py_ssize_t newsize){    //...前略    /* check for integer overflow */    if (new_allocated > PY_SIZE_MAX - newsize) {        PyErr_NoMemory();        return -1;    } else {        new_allocated += newsize;    }    if (newsize == 0)        new_allocated = 0;    items = self->ob_item;    if (new_allocated <= (PY_SIZE_MAX / sizeof(PyObject *)))        PyMem_RESIZE(items, PyObject *, new_allocated);    else        items = NULL;    if (items == NULL) {        PyErr_NoMemory();        return -1;    }    self->ob_item = items;    Py_SIZE(self) = newsize;    self->allocated = new_allocated;    return 0;}

PS:据说 Jython 是ArrayList<PyObject>,没看过源码不确定
热心网民 2小时前 0条评论
0 0

关于我们 联系我们招聘信息免责申明广告服务 网站地图 百度地图 TAG标签

Copyright@2018-2022 Cfgjzx.Com 财富国际在线 版权所有 All Rights Reserved   
财富国际提供:最新财富资讯、房产资讯、股票资讯、区块链、投资理财、保险导购、健康产品、公私募基金,易经等资讯及服务.