简体   繁体   中英

List index out of range when it should be in range

I have this program trying to add up the number of grid spaces around it of a certain value, and it keep giving the error "IndexError: list out of range". I've tried setting it to start on column and row late and end one column and row early to the same effect. The error points at [x+1][y+1] specifically.

for l in range(loops):
    for x in range(self.width):
        for y in range(self.height):
            neighbors = 0

            if tiles_copy[x-1][y-1] == 1:
                neighbors += 1
            if tiles_copy[x][y-1] == 1:
                neighbors += 1
            if tiles_copy[x+1][y-1] == 1:
                neighbors += 1
            if tiles_copy[x+1][y] == 1:
                neighbors += 1
            if tiles_copy[x+1][y+1] == 1:
                neighbors += 1
            if tiles_copy[x][y+1] == 1:
                neighbors += 1
            if tiles_copy[x-1][y+1] == 1:
                neighbors += 1
            if tiles_copy[x-1][y] == 1:
                neighbors += 1

Not an answer

Can you change the start of your loop to

print(self.width, len(tiles_copy))
for x in range(1, self.width):
    print(self.height, len(tiles_copy[x-1]), len(tiles_copy[x]), len(tiles_copy[x+1]))
    for y in range(1, self.height):

len(tiles_copy) should be equal to self.width and the 3 values in the loop should be equal to self.height. At a guess, some of the values are less.

You have to prevent non existing indexes like -1 and self.width, self.width + 1 etc...

I think it's easier to make a function that does the check for the 8 point around each xy combination

# =========================================
def count_neighbors(self, x , y):

    if self.width < 3 or self.height < 3:
        return 0

    neighbors = 0
    x_range = []
    y_range = []

    # get only the valid x and y indexes ------------
    if x > 0:
        if x < self.width - 1:
            x_range = range(x - 1, x + 1 + 1)   # x-1, x, x+1
        else:
            x_range = range(x - 1, x + 1)  # x-1, x
    else:
        # x == 0
        x_range = range(x, x + 1 + 1)   # x, x+1

    if y > 0:
        if y < self.width - 1:
            y_range = range(y - 1, y + 1 + 1)  # y-1, y, y+1
        else:
            y_range = range(y - 1, y + 1)    # y-1, y
    else:
        # y == 0
        y_range = range(y, y + 1 + 1)   #  y, y+1


    for x_index in x_range:
        for y_index in y_range:

            if x_range == x and y_index == y:
                # don't compare with itself
                continue

            if tiles_copy[x_index][y_index] == 1:
                neighbors += 1

    return neighbors


# ============================================
neighbors = 0
for l in range(loops):
    for x in range(self.width):
        for y in range(self.height):
            neighbors += count_neighbors(x, y)

It's a bit tricky You have to test and debug this yourself.

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