Hi Everyone,

I have a simple list reconstruction problem, but I don't really know
how to do it.

I have a list that looks like this:

l=[ ("A", "a", 1), ("A", "a", 2), ("A", "a", 3), ("A", "b", 1), ("A",
"b", 2), ("B", "a", 1), ("B", "b", 1)]

What I want to do is to reorganize it in groups, first by the middle
element of the tuple, and then by the first element. I'd like the
output look like this:

out=[
   [    #group by first element "A"
          [("A", "a", 1), ("A", "a", 2), ("A", "a", 3)], #group by
second element "a"
          [ ("A", "b", 1), ("A", "b", 2)], #group by second element
"b"
   ],
   [   #group by first element "B"
          [("B", "a", 1)],
          [("B", "b", 1)]
   ]
]


All the solutions I came up with are difficult to read and even harder
to go back and change, and I just feel are too complicated for such a
simple problem. I am wondering if anyone here has some insight.

If there is a 'functional' way to do this, it would be even greater.


Thanks,
Geoffrey

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to