はい。 CPythonのリストは単にポインタの配列です。 listobject.hに構造体の定義をチェックアウト:
https://hg.python.org/cpython/file/tip/Include/listobject.h#l22
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;
それがあなたを説得していない場合....
In [1]: import time
In [2]: import matplotlib.pyplot as plt
In [3]: def build_list(N):
...: start = time.time()
...: lst = [0]*N
...: stop = time.time()
...: return stop - start
...:
In [4]: x = list(range(0,1000000, 10000))
In [5]: y = [build_list(n) for n in x]
In [6]: plt.scatter(x, y)
Out[6]: <matplotlib.collections.PathCollection at 0x7f2d0cae7438>
In [7]: plt.show()
![enter image description here](https://i.stack.imgur.com/1gPM0.png)
割り当て 'ANSW = [0を] * NはNで線形になります。どうしてそんなことができないのですか?長さNのリストを作成しています。各要素は0に設定する必要があります。一定時間内にリストを作成する方法はありません。 –
代わりに 'collections.defaultdict(lambda max = max:max)'を使うことができます。ここで値のデフォルトは 'max'です。これは、操作ごとに平均償却O(1)で機能します。 – interjay
リストの乗算が*同じ*オブジェクトへの複数の参照を作成することを除いて。私は整数で何が起こるのかは分かりません。もちろん、N個の参照を作成する必要はありますが、原則的に*配列内にあるもののN個のコピーを作成するのと同じではありません。 – alexis