python数组横向合并_python中合并数组的方法

一、数组纵向合并

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_prev

concat[2 :, :] = xt

print(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_prev

concat[:, 3 :] = xt

print(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

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python,可以使用numpy库来进行数组横向合并。根据引用\[1\]和引用\[2\]的示例代码,可以使用np.hstack()函数来实现数组横向合并。具体步骤如下: 1. 导入numpy库:import numpy as np 2. 定义两个数组a和b: a = \[1, 2, 3, 4\] b = \[5, 6, 7, 8\] 3. 使用np.hstack()函数进行数组横向合并: mergeB = np.hstack((a, b)) 4. 打印合并后的结果: print("mergeB: ", mergeB) 根据引用\[1\]的示例代码,mergeB的结果将会是\[1 2 3 4 5 6 7 8\],即将数组a和数组b按照横向方向合并成一个新的数组。 #### 引用[.reference_title] - *1* [python多个数组的行合并以及列合并](https://blog.csdn.net/weixin_48506823/article/details/123916086)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Python多个数组合并及列合并的几个简单方法](https://blog.csdn.net/Vanhsy/article/details/69486241)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Python数组行列合并操作](https://blog.csdn.net/qq_54024805/article/details/130196576)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值