NumPy 中的 append函数

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

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

该函数采用以下参数。

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

arr

输入数组

2

values

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

来源:LearnFk无涯教程网

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

3

axis

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

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]]

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

技术教程推荐

赵成的运维体系管理课 -〔赵成〕

人人都能学会的编程入门课 -〔胡光〕

Go 并发编程实战课 -〔晁岳攀(鸟窝)〕

大厂晋升指南 -〔李运华〕

打造爆款短视频 -〔周维〕

程序员的个人财富课 -〔王喆〕

郭东白的架构课 -〔郭东白〕

现代React Web开发实战 -〔宋一玮〕

云原生架构与GitOps实战 -〔王炜〕

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