python - "Transpose" (rotate?) nested list -


i have list of lists of lists this:

[  [   [a,b],   [c,d]  ],  [   [e,f],   [g,h]  ] ] 

basically, cube of values.

what want different order of items in same cube, this:

[  [   [a,e],   [b,f]  ],  [   [c,g],   [d,h]  ] ] 

and, preferably, in one-liner (yes, know that's not best practice).

i know of map(list, *zip(a)) trick, couldn't figure out how apply here. lambdas , maps, probably?

upd: need --- i've done tests speeds of different sorting algorithms; each deepest list has values -- times sorting algorithms tested took. these lists in lists, represent different types of tests, , outer list has same thing repeated different test sizes. after such rotation, have list (test size) of lists (test type) of lists (sort type) of items (time), more convenient plot.

if understand correctly, want first transpose sublists transpose newly transposed groups:

print([list(zip(*sub)) sub in zip(*l)]) 

output:

in [69]: [list(zip(*sub)) sub in zip(*l)] out[69]: [[('a', 'e'), ('b', 'f')], [('c', 'g'), ('d', 'h')]] 

if want map foo lambda:

in [70]: list(map(list, map(lambda x: zip(*x), zip(*l)))) out[70]: [[('a', 'e'), ('b', 'f')], [('c', 'g'), ('d', 'h' 

for python2 don't need map call use itertools.izip initial transpose.:

in [9]: itertools import izip  in [10]: map(lambda x: zip(*x), izip(*l)) out[10]: [[('a', 'e'), ('b', 'f')], [('c', 'g'), ('d', 'h')]] 

Comments