【问题标题】:Matrix transpose using zip? [duplicate]使用zip的矩阵转置? [复制]
【发布时间】:2017-01-03 19:24:39
【问题描述】:

对 zip 很着迷,我突然想到可以将它用于矩阵转置。
这是python代码:

import sys
n = raw_input("enter the number of rows of the matrix:")
x = []
for i in range(int(n)):
    y = map(int,raw_input().split())
    x.append(y)
mat = zip(*x)

print ""

for i in mat:
    for j in i:
        sys.stdout.write(str(j))
        sys.stdout.write(" ")
    sys.stdout.write("\n")

但我对我的代码看起来不太满意。我该如何改进它?

谢谢!

编辑:

Input:
1 2
3 4
5 6
7 8

Output:
1 3 5 7 
2 4 6 8 

【问题讨论】:

    标签: python matrix transpose


    【解决方案1】:

    带 zip 的矩阵转置:

    a = [[1,2],[3,4]]
    
    a_t =list(map(list, zip(*a)))
    

    【讨论】:

      猜你喜欢
      • 2021-05-02
      • 2012-10-30
      • 1970-01-01
      • 1970-01-01
      • 2015-06-24
      • 2015-07-16
      • 1970-01-01
      • 2012-07-31
      • 1970-01-01
      相关资源
      最近更新 更多