通过矩阵迭代字典

时间:2017-06-28 18:00:12

标签: python numpy dictionary for-loop matrix

我有这个2矩阵:

$entity->setBrochure(new File($this->getParameter('brochures_directory').'/'.$entity->getBrochure()));

...我把它们放在字典中

x = np.matrix("1 2 3; 4 5 6")
y = np.matrix("7 8 9; 10 11 12")

现在我想要提取具有相同位置的矩阵的值,如下所示:1,7 ... 2,8 ... 3,9 ......依此类推,直到6,12(预期输出)。

我只是设法手动完成,如下所示:

d = {"a" : x, "b": y}

我正在尝试为此构建一个循环,但没有设法做到这一点。

有人可以帮我一把吗?

1 个答案:

答案 0 :(得分:4)

您可以执行以下操作:

values = zip(*d.values()) # gives [([1, 2, 3], [7, 8, 9]), ([4, 5, 6], [10, 11, 12])]
pairs = []
for value in values:
    pairs.extend(zip(*value)) #adds (1, 7), (2, 8), ... to pairs list

for pair in pairs:
    print(pair)

输出:

(1, 7)
(2, 8)
(3, 9)
(4, 10)
(5, 11)
(6, 12)
相关问题