在Python中使用for循环的2D网格

时间:2018-10-30 17:57:08

标签: python arrays

我想用python中的for循环创建一个2D网格。我可以这样简单地编写代码:

cols = 10
rows = 10
grid = [[0 for x in range(cols)] for y in range(rows)]
print(grid)

但是当我尝试在行中依次遍历i,然后在列中遍历j时,它显示错误:列表索引超出范围。不确定我的编码哪里出了问题?

rows = 10
cols = 10
i = 0
for i in range(rows):
    for j in range(cols):
        grid[i].append([j])
        i += 1

4 个答案:

答案 0 :(得分:1)

您要递增i两次。 第一次是for i in range(rows),第二次是i += 1。 删除第二条语句,使其看起来像这样:

cols = 10
rows = 10
grid = [[0 for x in range(cols)] for y in range(rows)]

for i in range(rows):
    for j in range(cols):
        grid[i].append([j])

代替:

for i in range(rows):
    for j in range(cols):
        grid[i].append([j])
        i += 1

当然也不需要此变量初始化:i = 0


如果您想找到另一种方法:

grid = [[0 for x in range(cols)] for y in range(rows)]

还必须使用嵌套数组初始化数组:

grid = []
for i in range(rows):
    grid.append([])
    for j in range(cols):
        grid[i].append(0)

答案 1 :(得分:1)

有几个问题:

  1. 在循环之前,您不会实例化一个空的grid列表。
  2. 您必须先将子列表添加到外部列表中,然后再尝试将其添加到外部列表中。
  3. 遍历range时,无需增加计数器。

这将起作用:

rows = 5
cols = 5

grid = []
for i in range(rows):
    grid.append([])
    for j in range(cols):
        grid[i].append(0)

print(grid)

[[0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0]]

答案 2 :(得分:1)

在使用grid[i].append()之类的内容之前,您需要创建一个空的子列表。因为最初列表中没有任何内容,所以您引用的内容不可用。因此,您的错误。 :(

您可以改为在每个外部迭代中创建一个子列表,并将0附加到内部迭代中的上一个子列表:

cols = 10
rows = 10

grid = []
for _ in range(rows):
    grid.append([])
    for _ in range(cols):
        grid[-1].append(0)

print(grid)

# [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
#  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
#  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
#  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
#  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
#  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
#  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
#  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
#  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
#  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]


将整体合并为一行:

grid = [[0 for _ in range(cols)] for _ in range(rows)]

答案 3 :(得分:0)

您的问题出在您的内部循环中:

for i in range(rows):
    for j in range(cols):
        grid[i].append([j])
        i += 1          # BAD!!

内部循环递增i 10次,使其超出范围。摆脱该声明,您可能会得到想要的。鉴于奇怪的编程风格,我不太确定您的构造应该做什么。没有多余的增量,您将单元素列表附加到原始的十个零:

[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, [0], [1], [2], [3], [4], [5], [6], [7], [8], [9]], 
 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, [0], [1], [2], [3], [4], [5], [6], [7], [8], [9]], 
 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, [0], [1], [2], [3], [4], [5], [6], [7], [8], [9]], 
 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, [0], [1], [2], [3], [4], [5], [6], [7], [8], [9]], 
 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, [0], [1], [2], [3], [4], [5], [6], [7], [8], [9]], 
 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, [0], [1], [2], [3], [4], [5], [6], [7], [8], [9]], 
 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, [0], [1], [2], [3], [4], [5], [6], [7], [8], [9]], 
 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, [0], [1], [2], [3], [4], [5], [6], [7], [8], [9]], 
 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, [0], [1], [2], [3], [4], [5], [6], [7], [8], [9]], 
 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, [0], [1], [2], [3], [4], [5], [6], [7], [8], [9]]]

您是否希望元素简单地为0-9?为此:

grid = [list(range(10)) for _ in range(10)]

[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 
 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 
 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 
 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 
 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 
 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 
 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 
 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 
 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 
 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]