我需要反问问题Iterator of list of list of iterables,也就是说,我需要撤消
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
我们注意到pivot()
实际上执行a
形状的圆形右移。定义
def unpivot(a):
return pivot(pivot(a))
可以工作,但是肯定有更好的解决方案来执行a
形状的圆形左移吗?
理想情况下,解决方案将是生成器,因为to_be_undone
的内存可能很大。
答案 0 :(得分:3)
尝试一下:
def unpivot(a):
return [tuple(map(list, zip(*l))) for l in zip(*a)]
测试:
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)
如果数据类型(元组或列表)无关紧要,这也将起作用:
def unpivot(a):
return [list(zip(*l)) for l in zip(*a)]