zoukankan      html  css  js  c++  java
  • Python小代码_5_二维矩阵转置

    使用列表推导式实现二维矩阵转置

    matrix = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
    print(matrix)
    matrix_t = [[row[col] for row in matrix] for col in range(len(matrix[0]))]
    print(matrix_t)
    
    #输出结果
    #[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
    #[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]

    使用内置函数 zip() 秩代、map() 和 list() 映射实现二维矩阵转置

    matrix = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
    print(matrix)
    matrix_t = list(map(list, zip(* matrix)))
    print(matrix_t)
    
    #输出结果
    #[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
    #[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]
  • 相关阅读:
    02-cocoapods的安装和使用
    01-唐巧之cocoapods
    class0513(html)
    程序集
    c#面向对象
    html
    dom
    Javascript
    Jquery
    ado.net
  • 原文地址:https://www.cnblogs.com/chuangming/p/8460036.html
Copyright © 2011-2022 走看看