数组拼接方法一

思路:首先将数组转成列表,然后利用列表的拼接函数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的更多相关文章

  1. [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 ...

  2. [Python Cookbook] Numpy Array Manipulation

    1. Reshape: The np.reshape() method will give a new shape to an array without changing its data. Not ...

  3. Python 将numpy array由浮点型转换为整型

    Python 将numpy array由浮点型转换为整型 ——使用numpy中的astype()方法可以实现,如:

  4. [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 ...

  5. python的numpy.array

    为什么要用numpy Python中提供了list容器,可以当作数组使用.但列表中的元素可以是任何对象,因此列表中保存的是对象的指针,这样一来,为了保存一个简单的列表[1,2,3].就需要三个指针和三 ...

  6. 【python】numpy array特殊数据统一处理

    array中的某些数据坏掉,想要统一处理,找到了这个方法,做个笔记. 比如,把数组中所有小于0的数字置为0 import numpy as np t = np.array([-2, -1, 0, 1, ...

  7. python 中 numpy array 中的维度

    简介 numpy 创建的数组都有一个shape属性,它是一个元组,返回各个维度的维数.有时候我们可能需要知道某一维的特定维数. 二维情况 >>> import numpy as np ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. 用session模拟登陆,手动输入验证码

    # 本练习是模拟登陆及验证码处理(把验证码下载到本地后手动输入) # 1 通过分析页面获得form表单的登陆接口为 action="https://www.douban.com/accoun ...

  2. Ubuntu 15.04 安装配置 Qt + SQLite3

    序 最近需要在Ubuntu下使用Qt开发项目,选择简单小巧的SQLite数据库,现将安装配置以及简单操作记录如下,以便日后查阅. 安装Qt CMake和Qt Creator是Linux下开发C++程序 ...

  3. nrf528xx bootloader 模块介绍(转载)

    转载https://www.cnblogs.com/rfnets/p/8205521.html 1. bootloader 的基本功能: 启动应用 几个应用之间切换 初始化外设 nordic nrf5 ...

  4. stm32启动地址

    理论上,CM3中规定上电后CPU是从0地址开始执行,但是这里中断向量表却被烧写在0x0800 0000地址里(Flash memory启动方式),那启动时不就找不到中断向量表了?既然CM3定下的规矩是 ...

  5. linux防火墙firewall使用简介

    1.firewalld的基本使用启动: systemctl start firewalld查看状态: systemctl status firewalld停止: systemctl disable f ...

  6. debian使用ibus

    $ sudo apt-get install ibus ibus-pinyin 点击右上角的键盘图标,设置拼音输入法

  7. debian使用sudo

    debian默认没有sudo ,在命令前无法使用sudo #切换到根用户$ su 输入根用户密码 # apt-get install sudo # nano /etc/sudoers 文件的 User ...

  8. category常量及对应字符串

  9. python week08 并发编程之多线程--理论部分

    一. 什么是线程 1.定义 线程就像一条工厂车间里的流水线,一个车间里可以用很多流水线,来执行生产每个零部件的任务. 所以车间可以看作是进程,流水线可以看作是线程.(进程是资源单位,线程是执行单位) ...

  10. 【SDOJ 3741】 【poj2528】 Mayor's posters

    Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral electio ...