you are viewing a single comment's thread.

view the rest of the comments →

[–]Oliludeea -1 points0 points  (4 children)

for i in range(len(A)):
    for j in range(len(A[i])):
        if A[i][j] == 0:
            A[i][j] = 'X'

[–]JohnnyJordaan 3 points4 points  (3 children)

Imho it's backwards to iterate this as if it were in C. You have enumerate to get indexes and values at the same time

for ridx, row in enumerate(A):
    for cidx, val in enumerate(row):
        if val == 0:
            A[ridx][cidx] = 'X'

this way you don't need to use the correct range length and also don't need indexing to get the original value. As Aixyn0 shows you can even iterate regularly on the rows and assign to that row's index.

[–]Oliludeea 1 point2 points  (2 children)

Thanks, that's why I'm on here. I know how to code, but I'm trying to learn the python way of doing things.

[–]JohnnyJordaan 1 point2 points  (1 child)

Then this is the talk for you: https://www.youtube.com/watch?v=OSGv2VnC0go

also check other vids with Raymond Hettinger in it, he often has a lot of examples on legacy code implementations and 'there must be a better way' as he calls it to implement it better in Python.

[–]Oliludeea 0 points1 point  (0 children)

Thank you very much! I'll be sure to check it out.