I have an 8x8 grid of different numbers, and I want to get the elements of the diagonal that contains a given starting position. Here is an example
l = [[str(randint(1,9)) for i in range(8)] for n in range(8)]
>> [
[1 5 2 8 6 9 6 8]
[2 2 2 2 8 2 2 1]
[9 5 9 6 8 2 7 2]
[2 8 8 6 4 1 8 1]
[2 5 5 5 4 4 7 9]
[3 9 8 8 9 4 1 1]
[8 9 2 4 2 8 4 3]
[4 4 7 8 7 5 3 6]
]
How would I go about getting the left diagonal from the position x=4 and y=3 (so 4th list and 5th element in that list)? So The diagonal I would want would be [8,2,2,4,5,8,9,4].
I essentially asked this question yesterday, and a user was able to help me with getting the right diagonal with this code:
def rdiagonal(m, x, y):
#x
row = max((y - x, 0))
#y
col = max((x - y, 0))
while row < len(m) and col < len(m[row]):
yield m[row][col]
row += 1
col += 1
I tried to change that code to get the left diagonal also, but could not figure it out. What changes would I need to make to get the left diagonal?