简体   繁体   中英

2D grid using for loop in Python

I want to create a 2D grid with for loop in python. I can do simple code like this:

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

But when I try to loop through i in rows and then j in columns, it shows error: list index out of range. Not sure where went wrong with my coding?

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

You are incrementing i twice. First time in for i in range(rows) , second time with i += 1 . Remove second statement so it will look like this:

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])

instead of:

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

Also this variable initialization is of course unnecesary: i = 0


If you want to find alternative way to do that:

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

You have to also initialize your array with nested array:

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

There a few issues:

  1. You don't instantiate an empty grid list prior to your loop.
  2. You have to append sublists to your outer list before trying to append to those sublists.
  3. When iterating over a range , there's no need to increment the counter.

So this will work:

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]]

You need to create an empty sublist before you use something like grid[i].append() . Because initially there is nothing in the list and you refer to something that is not available. Hence, your error. :(

You could instead create a sublist in each outer iteration and append 0 to previous sublist in the inner iteration:

cols = 10
rows = 10

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

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]]


Combining the whole to one line:

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

Your problem is in the increment in your inner loop:

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

The inner loop increments i 10 times, driving it out of range. Get rid of that statement, and you might get what you want. Given the strange programming style, I'm not quite sure what your construction is supposed to do. Without the superfluous increment, you're appending single-element lists to your original ten zeros:

[[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]]]

Did you want the elements to be simply 0-9? For that:

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]]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM