30秒学会 Python 片段 · 2024年1月2日

30秒学会 Python 片段 – Initialize 2D list

Initializes a 2D list of given width and height and value.

  • Use a list comprehension and range() to generate h rows where each is a list with length h, initialized with val.
  • Omit the last argument, val, to set the default value to None.

代码实现

def initialize_2d_list(w, h, val = None):
  return [[val for x in range(w)] for y in range(h)]

使用样例

initialize_2d_list(2, 2, 0) # [[0, 0], [0, 0]]

翻译自:https://www.30secondsofcode.org/python/s/initialize-2-d-list