重新排序压缩矩阵元素
Reorder Zipped Matrix Elements
我在可变大小的电子表格中有两个大小匹配的矩阵,一个包含数据项,另一个启用了标记项以供处理。压缩物品后,它们的处理顺序不方便。项目是如何压缩的,所以我同时拥有项目和启用?
预计只是 zipper = zip(output, doit)
但它严重失败,如输出所示。
预期输出和压缩矩阵如下所示。
from __future__ import print_function
from openpyxl import Workbook
wb = Workbook()
ws = wb.active
rows = [
['Number', 'Batch 1', 'Batch 2'],
[2, 40, 30],
[3, 40, 25],
[4, 50, 30],
[5, 30, 10],
]
enabled = [
[0, 0, 0],
[0, 1, 0],
[1, 1, 0],
[0, 0, 1],
[0, 1, 0],
]
for row in rows:
ws.append(row)
output = []
for i, row in enumerate(ws['B1:C5']):
output.append([])
for cell in row:
output[i].append(cell.value)
for row in enabled:
ws.append(row)
doit = []
for i, row in enumerate(ws['A6:B10']):
doit.append([])
for cell in row:
doit[i].append(cell.value)
zipper = zip(output, doit)
print(zipper)
for i in range(len(output[0])):
print(">>Do column")
for j in range(len(output)):
if doit[j][i]:
print(output[j][i])
输出
[([u'Batch 1', u'Batch 2'], [0, 0]), ([40, 30], [0, 1]), ([40, 25], [1, 1]), ([50, 30], [0, 0]), ([30, 10], [0, 1])]
>>Do column
40
>>Do column
30
25
10
我希望拉链看起来像:
[
[(0, u'Batch 1'), (0, u'Batch 2')]
[(0, 40), (1, 30)]
[(1, 40), (1, 25)]
[(0, 50), (0, 30)]
[(0, 30), (1, 10)]
]
没有成功:
# Flatten
zipper = zip(sum(output, []), sum(doit, []))
# Reassemble array
y = zip(*[iter(zipper)]*2)
print(list(y))
这应该有效
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 [a, b], [c, d] in zip(output, doit)
的 for 语句中进行赋值
我在可变大小的电子表格中有两个大小匹配的矩阵,一个包含数据项,另一个启用了标记项以供处理。压缩物品后,它们的处理顺序不方便。项目是如何压缩的,所以我同时拥有项目和启用?
预计只是 zipper = zip(output, doit)
但它严重失败,如输出所示。
预期输出和压缩矩阵如下所示。
from __future__ import print_function
from openpyxl import Workbook
wb = Workbook()
ws = wb.active
rows = [
['Number', 'Batch 1', 'Batch 2'],
[2, 40, 30],
[3, 40, 25],
[4, 50, 30],
[5, 30, 10],
]
enabled = [
[0, 0, 0],
[0, 1, 0],
[1, 1, 0],
[0, 0, 1],
[0, 1, 0],
]
for row in rows:
ws.append(row)
output = []
for i, row in enumerate(ws['B1:C5']):
output.append([])
for cell in row:
output[i].append(cell.value)
for row in enabled:
ws.append(row)
doit = []
for i, row in enumerate(ws['A6:B10']):
doit.append([])
for cell in row:
doit[i].append(cell.value)
zipper = zip(output, doit)
print(zipper)
for i in range(len(output[0])):
print(">>Do column")
for j in range(len(output)):
if doit[j][i]:
print(output[j][i])
输出
[([u'Batch 1', u'Batch 2'], [0, 0]), ([40, 30], [0, 1]), ([40, 25], [1, 1]), ([50, 30], [0, 0]), ([30, 10], [0, 1])]
>>Do column
40
>>Do column
30
25
10
我希望拉链看起来像:
[
[(0, u'Batch 1'), (0, u'Batch 2')]
[(0, 40), (1, 30)]
[(1, 40), (1, 25)]
[(0, 50), (0, 30)]
[(0, 30), (1, 10)]
]
没有成功:
# Flatten
zipper = zip(sum(output, []), sum(doit, []))
# Reassemble array
y = zip(*[iter(zipper)]*2)
print(list(y))
这应该有效
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 [a, b], [c, d] in zip(output, doit)