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
提交
取消