I'm having trouble unpacking a 2-dimensional list of tuples (or rather, I'm looking for a more elegant solution).
The list is as shown:
a = [ [(2, 3, 5), (3, 4, 6), (4, 5, 7), (1, 1, 1), (1, 2, 3)],
[(4, 9, 2), (8, 8, 0), (3, 5, 1), (2, 6, 8), (2, 4, 8)],
[(8, 7, 5), (2, 5, 1), (9, 2, 2), (4, 5, 1), (0, 1, 9)], ...]
And I want to unpack the tuples to obtain 3 nested lists of the same form, i.e.:
a_r = [ [2, 3, 4, 1, 1] , [4, 8, 3, 2, 2] , [8, 2, 9, 4, 0] , ...]
a_g = [ [3, 4, 5, 1, 2] , [9, 8, 5, 6, 4] , [7, 5, 2, 5, 1] , ...]
and so on. Here is my code:
a_r = []
a_g = []
a_b = []
for i in xrange(len(a)):
r0=[]
g0=[]
b0=[]
for j in range(5):
r0.append(a[i][j][0])
g0.append(a[i][j][1])
b0.append(a[i][j][2])
a_r.append(r0)
a_g.append(g0)
a_b.append(b0)
I'm sure there are more efficient ways to do this (I've just begun learning Python). This question is similar, but I wasn't able to follow the functional programming.
Thanks!