How to unpack tuples in nested list?

display_name

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!

John La Rooy
>>> 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)]]
>>> zip(*(zip(*x) for x in a))
[((2, 3, 4, 1, 1), (4, 8, 3, 2, 2), (8, 2, 9, 4, 0)), ((3, 4, 5, 1, 2), (9, 8, 5, 6, 4), (7, 5, 2, 5, 1)), ((5, 6, 7, 1, 3), (2, 0, 1, 8, 8), (5, 1, 2, 1, 9))]

>>> for row in _:
...     print row
... 
((2, 3, 4, 1, 1), (4, 8, 3, 2, 2), (8, 2, 9, 4, 0))
((3, 4, 5, 1, 2), (9, 8, 5, 6, 4), (7, 5, 2, 5, 1))
((5, 6, 7, 1, 3), (2, 0, 1, 8, 8), (5, 1, 2, 1, 9))

If it must be lists

>>> map(list, zip(*(map(list, zip(*x)) for x in a)))
[[[2, 3, 4, 1, 1], [4, 8, 3, 2, 2], [8, 2, 9, 4, 0]], [[3, 4, 5, 1, 2], [9, 8, 5, 6, 4], [7, 5, 2, 5, 1]], [[5, 6, 7, 1, 3], [2, 0, 1, 8, 8], [5, 1, 2, 1, 9]]]
>>> for row in _:
...     print row
... 
[[2, 3, 4, 1, 1], [4, 8, 3, 2, 2], [8, 2, 9, 4, 0]]
[[3, 4, 5, 1, 2], [9, 8, 5, 6, 4], [7, 5, 2, 5, 1]]
[[5, 6, 7, 1, 3], [2, 0, 1, 8, 8], [5, 1, 2, 1, 9]]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to unpack tuples in nested list?

From Dev

How to remove tuples nested on a list?

From Dev

How to unpack a nested list in python to sublists?

From Dev

How to unpack a map/list in scala to tuples for a variadic function?

From Dev

Django model field contains list of tuples - how to unpack in template

From Dev

How to unpack a Series of tuples in Pandas?

From Dev

How to round every float in a nested list of tuples

From Dev

How to makes tuples from a nested list

From Dev

How to convert a two nested list of lists into a nested list of tuples in Python?

From Dev

Python Nested tuples to list

From Dev

Unpack nested list for arguments to map()

From Dev

How to convert nested list of lists into a list of tuples in python 3.3?

From Dev

Python convert list of nested tuples to list of tuples

From Dev

coffeescript how to unpack like "tuples" in python

From Dev

How to unpack this list comprehension

From Dev

how to unpack the list of elements

From Dev

How to unpack nested DBRef in Python?

From Dev

single list comprehension to unpack nested dictionary

From Dev

Nested List of Lists to Single List of tuples

From Dev

How to 'unpack' a list or tuple in Python

From Dev

How to unpack items from a List?

From Dev

How to unpack a list in java to sublist

From Dev

How to unpack tupled list in python

From Dev

How to unpack items from a List?

From Dev

How to input a list of tuples

From Dev

How to sum a list of tuples

From Dev

How to create a list of tuples

From Dev

How to create a list of tuples

From Java

pandas: from how to unpack nested JSON as dataframe?