--- title: Python List Infinite Nesting fid: 20240110-114246 tags: list, infinite nesting, nesting --- (Python-List-Infinite-Nesting)= # List 无限嵌套 (Example) 2023-11-14 这个有趣的例子演示了一种用 Python list 实现无限俄罗斯套娃的方法,虽然并无实用价值。 为何能实现无限嵌套?这种无限嵌套消耗了多少资源? 如果能够实现任意有限(而不是无限)长度的数组,且又不怎么消耗资源,则是一个项非常有用的计算器。见: [](#myrange-simulation) ## 实现 `[[[[[...]]]]]` ```python title:"" a = [] a[0]=a ``` 等价的写法: ```python title:"" a = [] a.append(a) ``` test ```python title:"" a[0][0][0] == a id(a) == id(a[0][0][0]) ``` ## 实现 `[1[1[1[1...]]]]` ```python title:"" b = [1] b.append(b) ``` test ```python title:"" b[1][1][1] == b id(b[1][1][1]) == id(b) ``` 在以上的两个实例中,a 的长度为 1, b 的长度为 2,并没有消耗多少资源。