如何在 Python 中初始化和索引字典列表?

whe*_*ins 1 python dictionary list

目前我有一些看起来像这样的东西:

   if width < 101:
      column_placement_0[index] = rectangles
   elif width < 201:
      column_placement_1[index] = rectangles
   elif width < 301:
       column_placement_2[index] = rectangles
Run Code Online (Sandbox Code Playgroud)

... 等等。

相反,我希望能够只初始化一个存储字典的列数组。

首先,我觉得我会正常初始化一个数组......

columns = []
Run Code Online (Sandbox Code Playgroud)

但后来我不知道如何引用单个列表和字典索引可能类似于 Java 中的 2D 数组?

columns[index][dict_key] = value
Run Code Online (Sandbox Code Playgroud)

我该怎么办?

whe*_*ins 5

因此,尽管人们说我的代码可以正常工作,因为它是最终解决方案,但最终结果是:

对于初始化:

columns = [{} for i in range(10)]
Run Code Online (Sandbox Code Playgroud)

并存储一个值:

columns[i][key] = desired value
Run Code Online (Sandbox Code Playgroud)

我有点惊讶人们只是低估了我的问题。我做的 Python 越多,它就越像是一个非常常见的数据结构......