Python数组拼接np.concatenate实现过程
作者:落日峡谷 时间:2023-11-12 04:26:10
在python中,用于数组拼接的主要来自numpy包,当然pandas包也可以完成。
而,numpy中可以使用append和concatenate函数:
1. 建立数组
# pandas有专门的连接方法
import numpy as np
# np.size(a, 0) 行数
# np.size(a, 1) 列数
a = np.array([[1, 2],[3, 4],[5, 6]])
b = np.array([[11, 22],[33, 44],[55, 66]])
print(np.size(a,0))
print(np.size(a,1))
print(a)
print(b)
2. np.append函数
c = np.append(a,b)
print(c)
d = np.append(a,100) #直接将所有元素重新排列成新的一维数组
print(d)
3. np.concatenate函数
e = np.concatenate((a,b),axis=0) #简言之:行增加了;或者说在第一个中括号上添加元素
print(e)
f = np.concatenate((a,b),axis=1) #简言之:列增加了;或者说在第二个中括号上添加元素
print(f)
g = np.concatenate([a,b],axis=1) #简言之:列增加了;或者说在第二个中括号上添加元素
print(g)
注:同理,如果原数组是3维及以上,则会在相应的维度上添加元素,例如:3维 —— axis=2时,表示在第三个中括号([[[......]]]从外到内,一次为第一个中括号、第二个、第三个......)上添加元素。
来源:https://www.cnblogs.com/qi-yuan-008/p/12669541.html
标签:Python,数组,拼接,concatenate
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
关于PyTorch环境配置及安装教程(Windows10)
2021-04-23 09:04:31
![](https://img.aspxhome.com/file/2023/8/133368_0s.png)
python目标检测yolo2详解及预测代码复现
2021-09-30 12:35:20
![](https://img.aspxhome.com/file/2023/3/109873_0s.png)
Python如何实现自带HTTP文件传输服务
2023-01-05 05:04:15
![](https://img.aspxhome.com/file/2023/9/90289_0s.png)
Python实现将一个正整数分解质因数的方法分析
2021-01-09 10:39:29
理解python中装饰器的作用
2022-01-29 07:58:01
Python如何安装第三方模块
2023-08-01 12:50:07
Django框架 Pagination分页实现代码实例
2021-08-18 19:32:52
SQLServer查找字符串在另一字符串的索引位置
2024-01-13 06:10:30
![](https://img.aspxhome.com/file/2023/8/110968_0s.gif)
NumPy.npy与pandas DataFrame的实例讲解
2022-11-18 07:39:00
requests.gPython 用requests.get获取网页内容为空 ’ ’问题
2021-12-06 08:52:31
![](https://img.aspxhome.com/file/2023/9/97089_0s.png)
Facebook的特别之处是什么?
2008-08-04 12:57:00
![](https://img.aspxhome.com/file/UploadPic/20088/4/20088413183866s.gif)
Python中条件选择和循环语句使用方法介绍
2023-01-08 23:08:03
php自定义函数实现二维数组按指定key排序的方法
2023-06-27 09:54:36
如何解决attachEvent函数时,this指向被绑定的元素的问题?
2024-04-28 09:33:29
Golang学习之平滑重启
2024-04-25 15:05:57
![](https://img.aspxhome.com/file/2023/8/134228_0s.png)
Python中关于列表的常规操作范例以及介绍
2023-02-20 12:44:44
![](https://img.aspxhome.com/file/2023/6/133366_0s.png)
python实现简易云音乐播放器
2021-07-02 10:13:43
python-opencv-cv2.threshold()二值化函数的使用
2022-12-31 23:00:12
![](https://img.aspxhome.com/file/2023/2/92522_0s.png)
python数组过滤实现方法
2021-09-11 20:28:44
Java使用JDBC连接数据库
2024-01-22 03:21:45