题解 | 矩阵转置

矩阵转置

https://www.nowcoder.com/practice/7a8b443abda641bba5f70e0b4ebd8161

from typing import List, Union
import numpy as np
# 使用 Union 来表示类型可以是 int 或 float
def transpose_matrix(a: List[List[Union[int, float]]]) -> List[List[Union[int, float]]]:
    arr = np.array(a)
    transposed = arr.T
    return transposed.tolist()

# 处理输入输出
def main():
    try:
        matrix_str = input().strip()
        # 去掉最外层的方括号,并分割每个子数组
        rows = matrix_str[2:-2].split('],[')
        # 将每个子数组转换为数字列表
        matrix = [list(map(int, row.split(','))) for row in rows]
        
        # 计算转置矩阵
        result = transpose_matrix(matrix)
        
        # 格式化输出
        print(str(result).replace(' ', ''))
    except Exception as e:
        print(f"输入格式错误: {e}")

if __name__ == "__main__":
    main()

全部评论

相关推荐

迷茫的大四🐶:当你得到一些东西,那这些东西就会变成基本项,你有别人也有
点赞 评论 收藏
分享
Aurora23:属于挂一半,暂时进池子了,隔一段时间没有其他组捞的话就彻底结束了
点赞 评论 收藏
分享
昨天 18:25
门头沟学院 Java
点赞 评论 收藏
分享
评论
2
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务