NumPy 中的 append函数

首页 / Numpy入门教程 / NumPy 中的 append函数

此函数在输入数组的末尾添加值,输入数组的维度也必须匹配,否则将生成ValueError。

该函数采用以下参数。

numpy.append(arr, values, axis)
Sr.No. Parameter & 描述
1

arr

输入数组

2

values

要附加到arr。它的维度必须与arr相同

3

axis

沿其执行附加操作的轴。如果未给出,则将两个参数展平

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/numpy/numpy-append.html

来源:LearnFk无涯教程网

import numpy as np 
a = np.array([[1,2,3],[4,5,6]]) 

print 'First array:' 
print a 
print '\n'  

print 'Append elements to array:' 
print np.append(a, [7,8,9]) 
print '\n'  

print 'Append elements along axis 0:' 
print np.append(a, [[7,8,9]],axis = 0) 
print '\n'  

print 'Append elements along axis 1:' 
print np.append(a, [[5,5,5],[7,8,9]],axis = 1)

其输出如下-

First array:
[[1 2 3]
 [4 5 6]]

Append elements to array:
[1 2 3 4 5 6 7 8 9]

Append elements along axis 0:
[[1 2 3]
 [4 5 6]
 [7 8 9]]

Append elements along axis 1:
[[1 2 3 5 5 5]
 [4 5 6 7 8 9]]

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

邱岳的产品手记 -〔邱岳〕

大规模数据处理实战 -〔蔡元楠〕

深入浅出云计算 -〔何恺铎〕

物联网开发实战 -〔郭朝斌〕

A/B测试从0到1 -〔张博伟〕

Python自动化办公实战课 -〔尹会生〕

Spring编程常见错误50例 -〔傅健〕

如何成为学习高手 -〔高冷冷〕

Dubbo源码剖析与实战 -〔何辉〕

好记忆不如烂笔头。留下您的足迹吧 :)