python中合并數組的方法

來源:互聯網
上載者:User

標籤:參考   函數   detail   stack   print   edit   pytho   asp   AC   

一、數組縱向合并

1、使用np.vstack()函數

【code】

#數組a = [[1,2,3],[4,5,6]]b = [[1,1,1],[2,2,2]]#縱向合并c = np.vstack((a,b))print("c="+str(c))

【result】

c = array([[1, 2, 3],       [4, 5, 6],       [1, 1, 1],       [2, 2, 2]]  

 

2、使用 np.r_[]函數

【code】

#數組a = [[1,2,3],[4,5,6]]b = [[1,1,1],[2,2,2]]#縱向合并c =np.r_[a,b]
print("c="+str(c))

【result】

c = array([[1, 2, 3],       [4, 5, 6],       [1, 1, 1],       [2, 2, 2]]  

 

3、不使用函數,直接合并,見代碼

【code】

a_prev=np.array(([1,2],[3,4]))xt=np.array(([1,2],[3,4],[5,6]))concat = np.zeros([5,2])concat[: 2, :] = a_prevconcat[2 :, :] = xtprint(concat)

【result】

[[1. 2.] [3. 4.] [1. 2.] [3. 4.] [5. 6.]]

  

二、數組橫向合并  

1、使用np.hstack()函數

【code】

#數組a = [[1,2,3],[4,5,6]]b = [[1,1,1],[2,2,2]]#橫向合并
d = np.hstack((a,b))
print("d="+str(d))

【result】

d = array([[1, 2, 3, 1, 1, 1],       [4, 5, 6, 2, 2, 2]])

 

2、使用np.c_[]函數

【code】

#數組a = [[1,2,3],[4,5,6]]b = [[1,1,1],[2,2,2]]#橫向合并
d = np.c_[a,b]print("d="+str(d))

【result】

d = array([[1, 2, 3, 1, 1, 1],       [4, 5, 6, 2, 2, 2]])

 

3、不使用函數,直接合并,見代碼

【code】

a_prev=np.array(([1,2,3],[3,4,5]))xt=np.array(([4,5],[6,7]))concat = np.zeros([2,5])concat[:, : 3] = a_prevconcat[:, 3 :] = xtprint(concat)

【result】

[[1. 2. 3. 4. 5.] [3. 4. 5. 6. 7.]]

 

---------------------------------

參考:

1、http://blog.csdn.net/vanhsy/article/details/69486241

2、https://i.cnblogs.com/EditPosts.aspx?postid=8488878

 

python中合并數組的方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.