python矩阵转置和zip [英] python matrix transpose and zip
本文介绍了python矩阵转置和zip的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何得到这个矩阵的转置......任何更简单的算法方法来做到这一点......
第一个问题:
输入 a=[[1,2,3],[4,5,6],[7,8,9]]预期输出 a=[[1, 4, 7], [2, 5, 8], [3, 6, 9]]
第二个问题:
Zip 给了我下面的输出,当我不知道数组中有多少元素时,我如何压缩,在这种情况下我知道 3 个元素 a[0],a[1],a[2]但是我如何压缩 [n] 个元素
>>>zip(a[0],a[1],a[2])[(1, 4, 7), (2, 5, 8), (3, 6, 9)]
解决方案
问题解答:
<预><代码>>>>将 numpy 导入为 np>>>first_answer = np.transpose(a)>>>second_answer = [list(i) for i in zip(*a)]感谢 afg 的帮助
How to get the transpose of this matrix..Any easier ,algorithmic way to do this...
1st question:
Input a=[[1,2,3],[4,5,6],[7,8,9]]
Expected output a=[[1, 4, 7], [2, 5, 8], [3, 6, 9]]
2nd Question:
Zip gives me the following output said below,how can i zip when i dont know how many elements are there in the array,in this case i know 3 elements a[0],a[1],a[2] but how can i zip a[n] elements
>>> zip(a[0],a[1],a[2])
[(1, 4, 7), (2, 5, 8), (3, 6, 9)]
解决方案
question answers:
>>> import numpy as np
>>> first_answer = np.transpose(a)
>>> second_answer = [list(i) for i in zip(*a)]
thanks to afg for helping out
这篇关于python矩阵转置和zip的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文