维小维写作训练营python学习笔记

python:numpy数组的拼接-水平拼接和竖直拼接

2019-08-14  本文已影响0人  书生_Scholar

导入numpy,赋值t1和t2

In [1]:# 数据的拼接
In [2]:import numpy as np
In [3]:t1 = np.arange(12).reshape(2,6)
In [4]:t2 = np.arange(12,24).reshape(2,6)
In [5]:t1
Out[5]: 
array([[ 0,  1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10, 11]])
In [6]:t2
Out[6]: 
array([[12, 13, 14, 15, 16, 17],
       [18, 19, 20, 21, 22, 23]])
In [9]:np.hstack((t1,t2))                # 水平拼接
array([[ 0,  1,  2,  3,  4,  5, 12, 13, 14, 15, 16, 17],
       [ 6,  7,  8,  9, 10, 11, 18, 19, 20, 21, 22, 23]])
In [11]:np.hstack((t2,t1))                # t1和t2的位置不一样,拼接的结果也不一样
Out[11]: 
array([[12, 13, 14, 15, 16, 17,  0,  1,  2,  3,  4,  5],
       [18, 19, 20, 21, 22, 23,  6,  7,  8,  9, 10, 11]])
In [10]:np.vstack((t1,t2))               # 竖直拼接
Out[10]: 
array([[ 0,  1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10, 11],
       [12, 13, 14, 15, 16, 17],
       [18, 19, 20, 21, 22, 23]])
In [12]:np.vstack((t2,t1))                # t1和t2的位置不一样,拼接的结果也不一样
Out[12]: 
array([[12, 13, 14, 15, 16, 17],
       [18, 19, 20, 21, 22, 23],
       [ 0,  1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10, 11]])
上一篇下一篇

猜你喜欢

热点阅读