I have a nested list of 1's as follows:
rows = 8*"1".split()
nested_list = []
for i in range(8):
nested_list.append(rows)
The above produces
nested_list = [[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1]....x8]]
And I would like to change every other 1 to -1;
i.e I want the output to look like this: [[-1,1,1,1,1,1,1],[1,-1,1,1,1,1,1,1],[1,1,-1,1,1,1,1,1],[1,1,1,-1,1,1,1,1],...]
- I hope you can see what is happening.
Here is my code to try and achieve this:
for i in range(len(nested_list)):
for j in range(i,len(nested_list)):
nested_list[i][i] = "-1"
break
However, this produces [[-1,-1,-1,-1,-1,-1,-1,-1],[-1,-1,-1,-1,-1,-1,-1,-1,-1],...x8]]
But I don't understand why?
My logic for the above code is that 'i' will iterate from 0 to 7
; and for each iteration, j will be equal to 'i'
and we then change the position nested_list[I][i] to -1
, i.e. nested_list[1][1]
, then nested_list[2][2]
, then nested_list[3][3]
,..., nested_list[8][8]
, which is the diagonals
of the 2-D array? Where is my logic incorrect?? Thanks!