1

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!

Community
  • 1
  • 1
  • 1
    Have you tried anything so far? – kartikg3 Dec 09 '14 at 03:33
  • 1
    Dont understand how you are joint/unpacking the tuples? Can you clarify that? – Marcin Dec 09 '14 at 03:34
  • What troubles are you facing exactly? How much progress have you made? Try and be as specific as possible. Stackoverflow is meant to help you help yourself, not to spoon-feed you :) – shridharama Dec 09 '14 at 03:34
  • If the point is to have a structured, "rectangular" multi-dimensional array of numbers, and you basically want to "rearrange the axes", you might want to look into Numpy. – Karl Knechtel Dec 09 '14 at 03:56

3 Answers3

4

I think you are after something like this:

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)]]

for row in a:
    print(list(zip(*row)))

Which gives:

[(2, 3, 4, 1, 1), (3, 4, 5, 1, 2), (5, 6, 7, 1, 3)]
[(4, 8, 3, 2, 2), (9, 8, 5, 6, 4), (2, 0, 1, 8, 8)]
[(8, 2, 9, 4, 0), (7, 5, 2, 5, 1), (5, 1, 2, 1, 9)]

The resulting tuples are same as in your example, but different order. I dont understand how you ordered them. If you could clarify this, I might modify the example.

Hope this helps.

Marcin
  • 215,873
  • 14
  • 235
  • 294
3
>>> 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]]
John La Rooy
  • 295,403
  • 53
  • 369
  • 502
0

Tuples are immutable (meaning you cannot change them), so you're going to have to go through every element to extract them. That is essentially what the functional programming example you linked to is doing, in a very pythonic, list comprehension, way.

To help you help yourself, as shirdharama says, take a look at the example again and know that fmap_lol really means "function mapping - list of lists", fmap_list means "function mapping - list", and fmap_el means "function mapping - element". So split_nt calls fmap_lol, which splits your input in a list of lists (the tuples), calls fmap_list, which takes each list and calls fmap_el, which returns the element back up the chain.

The example is just a way to walk through the tuples to extract every element and put them where you wish. It's functional because they are functions calls, not a single line of later forgotten gobbligook.

If you're unfamiliar with list comprehensions, lamda (or anonymous functions), or anything else in the example I suggest spending some time with the python documentation and looking each up.

Todd Vanyo
  • 545
  • 5
  • 15