美文网首页维小维写作训练营python学习笔记
python:numpy中数组的行列交换

python:numpy中数组的行列交换

作者: 书生_Scholar | 来源:发表于2019-08-14 09:48 被阅读0次

    导入库,赋值t

    In [1]:import numpy as np
    In [2]:t = t = np.arange(12,24).reshape(3,4)
    Out[2]: 
    array([[12, 13, 14, 15],
           [16, 17, 18, 19],
           [20, 21, 22, 23]])
    
    • 1、行交换
    In [15]:t[[1,2],:] = t[[2,1],:]                  # 第2行和第3行交换
    In [16]:t
    Out[16]: 
    array([[12, 13, 14, 15],
           [20, 21, 22, 23],                         #  第3行已换到第2行
           [16, 17, 18, 19]])                        #  第2行已换到第3行
    

    2、列交换

    In [17]:t[:,[0,2]] = t[:,[2,0]]             # 第1列和第3列交换
    In [18]:t
    Out[18]: 
    array([[14, 13, 12, 15],
           [22, 21, 20, 23], 
           [18, 17, 16, 19]])
    

    相关文章

      网友评论

        本文标题:python:numpy中数组的行列交换

        本文链接:https://www.haomeiwen.com/subject/hiywjctx.html