[CentralOH] Manipulating Nested Sequences

Steven Huwig steven_h at acm.org
Thu Jan 15 21:39:42 CET 2009


I don't know what this is called. I think this does what you want.

import itertools
import operator
def foo(x):
    outarray = []
    try:
        for i in itertools.count():
            outrow = []
            for row in x:
                outrow.append(map(operator.itemgetter(i), row))
            outarray.append(outrow)
    except IndexError:
        pass
    return outarray

-- Steve

On Thu, Jan 15, 2009 at 2:35 PM, Mark Erbaugh <mark at microenh.com> wrote:
> I have some data in the form of nested sequences. Think of this as a
> three-dimensional array.
>
> data[country][category][product]
>
> Though not required by Python, the each data element at each level is
> the same length as would be required by a multi-dimensional array in
> other languages.
>
> I would like to rearrange the data so it is in the format of
>
> data[product][country][category]
>
> for example if the data were:
>
> (
> ((A,a),(B,b),(C,c)),
> ((D,d),(E,e),(F,f)),
> ((G,g),(H,h),(I,i)),
> ((J,j),(K,k),(L,l))
> )
>
> I would like to transform it to
>
> (
> ((A,B,C),(D,E,F),(G,H,I),(J,K,L)),
> ((a,b,c),(d,e,f),(g,h,i),(j,k,l)),
> )
>
> Is there a name for this particular operation?
>
> Is there a generic way to do this for arrays with an arbitrary number of
> dimensions?
>
> _______________________________________________
> CentralOH mailing list
> CentralOH at python.org
> http://mail.python.org/mailman/listinfo/centraloh
>


More information about the CentralOH mailing list