[Python Cookbook] Numpy Array Joint Methods: Append, Extend & Concatenate
数组拼接方法一
思路:首先将数组转成列表,然后利用列表的拼接函数append()、extend()等进行拼接处理,最后将列表转成数组。
示例1:
import numpy as np
a=np.array([1,2,5])
b=np.array([10,12,15])
a_list=list(a)
b_list=list(b)
a_list.extend(b_list)
a_list
[1, 2, 5, 10, 12, 15]
a=np.array(a_list)
a
array([ 1, 2, 5, 10, 12, 15])
该方法只适用于简单的一维数组拼接,由于转换过程很耗时间,对于大量数据的拼接一般不建议使用。
数组拼接方法二
思路:numpy提供了numpy.append(arr, values, axis=None)函数。对于参数规定,要么一个数组和一个数值;要么两个数组,不能三个及以上数组直接append拼接。append函数返回的始终是一个一维数组。
示例2:
a=np.arange(5)
a
array([0, 1, 2, 3, 4])
np.append(a,10)
array([ 0, 1, 2, 3, 4, 10])
a
array([0, 1, 2, 3, 4])
b=np.array([11,22,33])
b
array([11, 22, 33])
np.append(a,b)
array([ 0, 1, 2, 3, 4, 11, 22, 33])
a
array([[1, 2, 3],
[4, 5, 6]])
b=np.array([[7,8,9],[10,11,12]])
b
array([[ 7, 8, 9],
[10, 11, 12]])
np.append(a,b)
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
numpy的数组没有动态改变大小的功能,numpy.append()函数每次都会重新分配整个数组,并把原来的数组复制到新数组中。
数组拼接方法三
思路:numpy提供了numpy.concatenate((a1,a2,...), axis=0)函数。能够一次完成多个数组的拼接。其中a1,a2,...是数组类型的参数
示例3:
a=np.array([1,2,3])
b=np.array([11,22,33])
c=np.array([44,55,66])
np.concatenate((a,b,c),axis=0) # 默认情况下,axis=0可以不写
array([ 1, 2, 3, 11, 22, 33, 44, 55, 66]) #对于一维数组拼接,axis的值不影响最后的结果
a=np.array([[1,2,3],[4,5,6]])
b=np.array([[11,21,31],[7,8,9]])
np.concatenate((a,b),axis=0)
array([[ 1, 2, 3],
[ 4, 5, 6],
[11, 21, 31],
[ 7, 8, 9]])
np.concatenate((a,b),axis=1) #axis=1表示对应行的数组进行拼接
array([[ 1, 2, 3, 11, 21, 31],
[ 4, 5, 6, 7, 8, 9]])
对numpy.append()和numpy.concatenate()两个函数的运行时间进行比较
示例4:
from time import clock as now
a=np.arange(9999)
b=np.arange(9999)
time1=now()
c=np.append(a,b)
time2=now()
print time2-time1
28.2316728446
a=np.arange(9999)
b=np.arange(9999)
time1=now()
c=np.concatenate((a,b),axis=0)
time2=now()
print time2-time1
20.3934997107
可知,concatenate()效率更高,适合大规模的数据拼接
数组拼接方法四
Use vstack to stack arrays in sequence vertically (row wise).
p = np.ones([2, 3], int)
np.vstack([p, 2*p])
Output:
array([[1, 1, 1],
[1, 1, 1],
[2, 2, 2],
[2, 2, 2]])
Use hstack to stack arrays in sequence horizontally (column wise).
np.hstack([p, 2*p])
Output:
array([[1, 1, 1, 2, 2, 2],
[1, 1, 1, 2, 2, 2]])
-----------------------------------------------
作者:故乡月zyl
来源:CSDN
原文:https://blog.csdn.net/zyl1042635242/article/details/43162031
[Python Cookbook] Numpy Array Joint Methods: Append, Extend & Concatenate的更多相关文章
- [Python Cookbook] Numpy Array Slicing and Indexing
1-D Array Indexing Use bracket notation [ ] to get the value at a specific index. Remember that inde ...
- [Python Cookbook] Numpy Array Manipulation
1. Reshape: The np.reshape() method will give a new shape to an array without changing its data. Not ...
- Python 将numpy array由浮点型转换为整型
Python 将numpy array由浮点型转换为整型 ——使用numpy中的astype()方法可以实现,如:
- [Python Cookbook] Numpy: Multiple Ways to Create an Array
Convert from list Apply np.array() method to convert a list to a numpy array: import numpy as np myl ...
- python的numpy.array
为什么要用numpy Python中提供了list容器,可以当作数组使用.但列表中的元素可以是任何对象,因此列表中保存的是对象的指针,这样一来,为了保存一个简单的列表[1,2,3].就需要三个指针和三 ...
- 【python】numpy array特殊数据统一处理
array中的某些数据坏掉,想要统一处理,找到了这个方法,做个笔记. 比如,把数组中所有小于0的数字置为0 import numpy as np t = np.array([-2, -1, 0, 1, ...
- python 中 numpy array 中的维度
简介 numpy 创建的数组都有一个shape属性,它是一个元组,返回各个维度的维数.有时候我们可能需要知道某一维的特定维数. 二维情况 >>> import numpy as np ...
- [Python Cookbook] Numpy: Iterating Over Arrays
1. Using for-loop Iterate along row axis: import numpy as np x=np.array([[1,2,3],[4,5,6]]) for i in ...
- [Python Cookbook] Numpy: How to Apply a Function to 1D Slices along the Given Axis
Here is a function in Numpy module which could apply a function to 1D slices along the Given Axis. I ...
随机推荐
- 如何用纯 CSS 创作一副国际象棋
效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/WyXrjz 可交互视频 ...
- manjaro18 配置国内镜像源
1.配置镜像源: sudo pacman-mirrors -i -c China -m rank 2.设置 archlinuxcn 源: sudo nano /etc/pacman.conf 添加以下 ...
- python文件打包为exe可执行文件的方法
我自己常用Pyinstaller库打包 第一步: 安装pyinstaller库 pip install pyinstaller 第二步: 在py文件所在目录输入 mydemo.py是自己写的py文 ...
- loadView、viewDidLoad及viewDidUnload的关系(转)
本文目录 一.loadView 二.viewDidLoad 三.viewDidUnload 四.三个方法的关系 标题中所说的3个方法,都是UIViewController的方法,跟UIViewCont ...
- list_for_each_entry()函数分析
list_for_each原型: #define list_for_each(pos, head) \ for (pos = (head)->next, prefetch(pos->nex ...
- 串口编程的相关API函数
用户使用函数CreateFile()创建与指定串口相关联的文件,然后可以使用该函数返回的文件句柄进行串口参数设置.• 01 HANDLE hModem; //定义串口句柄02 hModem=Creat ...
- 装饰器与lambda
装饰器 实际上理解装饰器的作用很简单,在看core python相关章节的时候大概就是这种感觉.只是在实际应用的时候,发现自己很难靠直觉决定如何使用装饰器,特别是带参数的装饰器,于是摊开来思考了一番, ...
- JAVA-基础(五) 更多工具集
1.StringTokenizer(字符串标记) StringTokenizer实现枚举(Enumeration)接口.因此,给定一个输 入字符串,可以使用StringTokenizer对包含于其中的 ...
- java_时间戳与Date_相互转化
[转自:http://blog.csdn.net/heng615975867/article/details/36016617] 1.时间戳的定义 时间戳是指文件属性里的创建.修改.访问时间. 数字时 ...
- luogu4173 残缺的字符串
there #include <algorithm> #include <iostream> #include <cstring> #include <cst ...