简体   繁体   中英

Performing arithmetic on list indexes

I have a list of lists (called matrix) which represents row number and column numbers on the screen respectively. To draw to the screen using numbers from the list, I use this loop:

for rows in matrix:
    for columns in rows:
        if rows[columns] == 0:
            pygame.draw.rect(screen, WHITE, (0 + (row * 20), 0 + (column * 20), 20, 20))

However, when running this loop the number in this position of the array is multiplied by 20, instead of the index of the position.

So my question is, how do I let Python perform arithmetic on the index of an array and not on the value that is paired with that index?

I hope my problem is clear. To be honest my question even confuses myself, but I can't find another way to word it.

To get the indices, you can use enumerate :

for row_index, row in enumerate(matrix):
    for col_index, col in enumerate(row):
        ...

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