这应该管用
zipper = [[(a, c), (b, d)] for [a, b], [c, d] in zip(output, doit)]
inverse_zipper = [[(c, a), (d, b)] for [a, b], [c, d] in zip(output, doit)]
作为旧线路的替代品
zipper = zip(output, doit)
标准类型
[]
表示
list
和
()
一
tuple
. python类型的文档是
here
. 主要的区别是元组是不可变的。在这里,我尊重你想要的输出
列表理解
zipper = [[(a, c), (b, d)] for [a, b], [c, d] in zip(output, doit)]
等于
zipper = []
for [a, b], [c, d] in zip(output, doit):
zipper.append([(a, c), (b, d)])
拆包
解包是一种快速的任务。
a, b = [4,7]
等于
some_list = [4,7]
a = some_list[0]
b = some_list[1]
两者都分配4到A和7到B
你知道的输出
zip(output, doit)
是
[([u'Batch 1', u'Batch 2'], [0, 0]), ([40, 30], [0, 1]), ([40, 25], [1, 1]), ([50, 30], [0, 0]), ([30, 10], [0, 1])]
所以如果你这样做的话
for row in zip(output, doit):
,
row
将采用以下形式
([40, 30], [0, 1])
可以解包为
[a, b], [c, d]
你可以直接在你的for语句中进行分配
for [a, b], [c, d] in zip(output, doit)