I have an array:
X = [[5*, 0, 0, 0, 0, 0, 0, 0],
[9*, 6, 0, 0, 0, 0, 0, 0],
[4, 6*, 8, 0, 0, 0, 0, 0],
[0, 7*, 1, 5, 0, 0, 0, 0],
[9, 3, 3*, 4, 4, 0, 0, 0],
[4, 5, 5*, 6, 7, 5, 0, 0],
[4, 5, 6, 8*, 7, 7, 8, 0],
[4, 7, 8, 9*, 7, 3, 9, 6]]
I want to select and append all the values that are marked by *. The approach is basically to select 0th element from 0th and 1st row...1th element from 2nd and 3rd row..and so on.
The resulting set should be:
Result = ((X[0][0], (X[1][0]), (X[2][1], X[3][1]), (X[4][2], X[5][2]), (X[6][3], X[7][3]))
Which can be written as:
Result = ((X[n+0][n], (X[n+1][n]), (X[n+2][n+1], X[n+3][n+1]), (X[n+4][n+2], X[n+5][n+2]), (X[n+6][n+3], X[n+7][n+3]))
Where n = 0
How do I do that? I have applied this but its not working:
Result = []
for a in X:
Result.append([[[ a[i][j] ] for i in range(0,8)] for j in range(0,8)])
But no results. Any guesses?
This will work if X
has an even number of lists in it:
>>> [(X[2*i][i], X[2*i+1][i]) for i in range(len(X)//2)]
[(5, 9), (6, 7), (3, 5), (8, 9)]
If you don't mind flattened lists, then then will work for X
of any length:
>>> [lst[idx//2] for idx, lst in enumerate(X)]
[5, 9, 6, 7, 3, 5, 8, 9]