列表和数组在存储值时有何不同?

In what manner do list and array differ while storing values?

我正在读 O'Reilly 出版的 Luciano Ramalho 的 Fluent Python 一本书。在第二章中,作者提出了一个有趣的定义容器序列和平面序列的语句。

Container sequences: list, tuple, and collections.deque can hold items of different types.

Flat sequences: str, bytes, bytearray, memoryview, and array.array hold items of one type.

然后他继续说:-

Container sequences hold references to the objects they contain, which may be of any type, while flat sequences physically store the value of each item within its own memory space, and not as distinct objects. Thus, flat sequences are more compact, but they are limited to holding primitive values like characters, bytes, and numbers.*

这让我想到,如果列表存储对它们正在存储的对象的引用,那么像数组这样的平面序列以什么方式存储其元素的值?作者说平面序列物理存储每个项目的值。他这是什么意思?

这是我第一次提问。请原谅我在这方面的知识不足。

只是为了演示,假设 int 不是原始类型。那么一个包含值本身的数组就像这样

Address  0xaddr0  0xaddr1  0xaddr2
        +--------+--------+--------+
Content |   69   |  420   |  1337  |
        +--------+--------+--------+

持有地址(参考)的列表会像这样

Address   0xaddr0   0xaddr1   0xaddr2    0xaddr3   0xaddr4   0xaddr5
        +---------+---------+---------+
Content | 0xaddr3 | 0xaddr4 | 0xaddr5 |    69        420      1337
        +---------+---------+---------+    ^          ^        ^
             |         |         |         |          |        |
             +---------+---------+---------+          |        |
                       |         |                    |        |
                       +---------+--------------------+        |
                                 |                             |
                                 +-----------------------------+

因此,当您从数组中获取值时,您会立即获取;但是对于列表,你得到那个值的地址,然后去那个地址实际得到这个值。