我如何构造此for循环?

问题描述 投票:0回答:2

我有这行代码,我想将其用于其他功能:

array=[[0 for column in range(width)] for row in range(height)]

如何分隔for语句?我试过了:

    column = 0
    for row in range(height):
        row = 0
        array = [column[row]]
        return array

输出应为以0为元素的列表的列表。

python loops
2个回答
1
投票

您可以尝试这个

array = []
for row in range(height):
  array.append([0 for column in range (width)])

1
投票

怎么样

array = [[0] * height] * width

0
投票

您将需要显式定义数组变量,即

rows = []
for row in range(height):
    column = [0] * width
    rows.append(column)
© www.soinside.com 2019 - 2024. All rights reserved.