将单个元素添加到numpy中的数组 [英] Add single element to array in numpy

查看:31
本文介绍了将单个元素添加到numpy中的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 numpy 数组,其中包含:

I have a numpy array containing:

[1, 2, 3]

我想创建一个包含以下内容的数组:

I want to create an array containing:

[1, 2, 3, 1]

也就是说,我想将第一个元素添加到数组的末尾.

That is, I want to add the first element on to the end of the array.

我已经尝试过显而易见的:

I have tried the obvious:

np.concatenate((a, a[0]))

但我收到一个错误提示 ValueError: arrays must have same number of维数

But I get an error saying ValueError: arrays must have same number of dimensions

我不明白这一点 - 数组都只是一维数组.

I don't understand this - the arrays are both just 1d arrays.

推荐答案

append() 创建一个新数组,该数组可以是带有附加元素的旧数组.

append() creates a new array which can be the old array with the appended element.

我认为使用适当的方法添加元素更正常:

I think it's more normal to use the proper method for adding an element:

a = numpy.append(a, a[0])

这篇关于将单个元素添加到numpy中的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆