Reputation: 15
I need the reverse of question Iterator of list of list of iterables which is to say, that I need to undo
def pivot(a):
return zip(*(zip(*l) for l in a))
def unpivot(a):
return <INSSERT_CODE_HERE>
original = [
(list(range(1,10)), list(range(11,20)), list(range(21,30))),
(list(range(101,110)), list(range(111,120)), list(range(121,130)))
]
to_be_undone = pivot(original)
original = unpivot(to_be_undone)
original == [
([1, 2, 3, 4, 5, 6, 7, 8, 9], [11, 12, 13, 14, 15, 16, 17, 18, 19], [21, 22, 23, 24, 25, 26, 27, 28, 29]),
([101, 102, 103, 104, 105, 106, 107, 108, 109], [111, 112, 113, 114, 115, 116, 117, 118, 119], [121, 122, 123, 124, 125, 126, 127, 128, 129])
]
# shape: 2 x 3 x 10
to_be_undone = [
((1, 11, 21), (101, 111, 121)),
((2, 12, 22), (102, 112, 122)),
((3, 13, 23), (103, 113, 123)),
((4, 14, 24), (104, 114, 124)),
((5, 15, 25), (105, 115, 125)),
((6, 16, 26), (106, 116, 126)),
((7, 17, 27), (107, 117, 127)),
((8, 18, 28), (108, 118, 128)),
((9, 19, 29), (109, 119, 129))
]
# shape: 10 x 2 x 3
We notice that pivot()
actually performs a circular right shift of a
's shape. Defining
def unpivot(a):
return pivot(pivot(a))
would work but surely there is a better solution for performing a circular left shift of a
's shape?
Ideally, the solution would be a generator since to_be_undone
could be quite big in memory.
Upvotes: 0
Views: 39
Reputation: 16633
Try this:
def unpivot(a):
return [tuple(map(list, zip(*l))) for l in zip(*a)]
Test:
original == [
([1, 2, 3, 4, 5, 6, 7, 8, 9], [11, 12, 13, 14, 15, 16, 17, 18, 19], [21, 22, 23, 24, 25, 26, 27, 28, 29]),
([101, 102, 103, 104, 105, 106, 107, 108, 109], [111, 112, 113, 114, 115, 116, 117, 118, 119], [121, 122, 123, 124, 125, 126, 127, 128, 129])
]
to_be_undone = [
((1, 11, 21), (101, 111, 121)),
((2, 12, 22), (102, 112, 122)),
((3, 13, 23), (103, 113, 123)),
((4, 14, 24), (104, 114, 124)),
((5, 15, 25), (105, 115, 125)),
((6, 16, 26), (106, 116, 126)),
((7, 17, 27), (107, 117, 127)),
((8, 18, 28), (108, 118, 128)),
((9, 19, 29), (109, 119, 129))
]
print(unpivot(to_be_undone) == original)
If data type (tuple or list) doesn't matter, this will also work:
def unpivot(a):
return [list(zip(*l)) for l in zip(*a)]
Upvotes: 3