Python的列表是如何实现的?是链表还是数组?我四处搜寻,只发现有人在猜测。我的C知识还不够好,不能看源代码。
3 回答
慕尼黑8549860
TA贡献1818条经验 获得超11个赞
...>python -m timeit --setup="x = [None]*1000" "x[500]"10000000 loops, best of 3: 0.0579 usec per loop...> python -m timeit --setup="x = [None]*1000" "x[0]"10000000 loops, best of 3: 0.0566 usec per loop
拉风的咖菲猫
TA贡献1995条经验 获得超2个赞
listobject.h
typedef struct { PyObject_HEAD Py_ssize_t ob_size; /* 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 */ Py_ssize_t allocated;} PyListObject;
PyObject_HEAD
listobject.c
new_allocated = (newsize >> 3) + (newsize < 9 ? 3 : 6);new_allocated += newsize;
newsize
allocated + 1
extend
append
添加回答
举报
0/150
提交
取消