首页 > 解决方案 > python中对二维列表元素的内存引用是否连续?

问题描述

Python 使用内存中对实际元素的引用(即,如果您可能会说的指针)来实现列表。这些引用在内存中是连续的。例子:

# RefX, RefY and RefZ are contiguous memory references to the following elements
list_example = [elementX, elementY, elementZ] 

#insert elementS at (index 1) will shift all the next references (RefY and RefZ in this 
 case) in the memory (assume each ref is  8 bit size)

list_example = [elementX, elementS, elementY, elementZ]

#Therefore insert operation time complexity is O(N) 

我的问题是:

如果是二维列表:如果我将一个元素附加到第一个列表的末尾:

list_of_lists[0].append(item) 

这种情况会导致内存中第二个和第三个列表中元素的引用发生变化吗?

我的主要观点:我问是因为我想知道在追加或弹出的情况下,就时间复杂度而言,实现三个堆栈的列表(作为列表列表)是否是昂贵的操作?

标签: pythonpython-3.xstack

解决方案


推荐阅读