简体   繁体   English

如何在python中将数组追加到数组

[英]How to append an array to an array in python

I'm trying to append an array new to an existing empty array ra like this: 我试图像这样将的数组追加到现有的空数组ra上

ra.append(new)

However, when I run this code and write ra , it looks like this: 但是,当我运行此代码并编写ra时 ,它看起来像这样:

ra = [array([ 103.290577,  103.312447,  103.371779,  103.376812,  103.38486 ,...]]

All of my values are appending correctly and I am getting a 2d array, but there is this string "array" at the beginning of every array inside ra . 我所有的值都正确附加,得到2d数组,但是ra内每个数组的开头都有这个字符串“ array”。 What is causing this and how can I correct my problem? 是什么原因引起的,如何解决我的问题?

I definitely need to use append as I need an array of arrays. 我肯定需要使用append,因为我需要一个数组数组。 My problem is when I write ra , I get this string "array(" in front of every new array. Why is this happening is my question. 我的问题是当我写ra时 ,在每个新数组的前面都得到了这个字符串“ array(”)。为什么会这样呢?

Are you looking for ra.extend(new) ? 您在寻找ra.extend(new)吗? Documentation here 这里的文件

Edit: OP's comment: From what I have read, I thought that extend would add my values, but I would have one long array. 编辑: OP的评论: 从我读过的内容来看,我认为扩展将添加我的值,但是我将有一个长数组。 I need a 2d array of these values. 我需要这些值的二维数组。

Yes extend() will create one long array from elements of ra and new .AFAIK no inbuilt function is available for create a 2d array from two 1d arrays. 是的,extend()将根据ranew元素创建一个长数组。AFAIK没有内置函数可用于从两个1d数组创建2d数组。 Although if you have all numbers in ra and new, you can use numpy and use column_stack or vstack to stack ra and new together to create a 2d array. 但如果你有在RA和新的所有号码,你可以使用numpy的和使用column_stackvstackranew共同创造一个二维数组。

My problem is when I write ra, I get this string "array(" in front of every new array. Why is this happening is my question. 我的问题是,当我编写ra时,在每个新数组的前面都得到了这个字符串“ array(”。为什么会这样呢?

You do not need to worry about this, it just means that your list was initialized as an array from some import functionality. 您不必为此担心,它只意味着您的列表已通过某些导入功能初始化为数组。 Not sure what that is without a larger code sample, but it looks like numpy or array . 不知道没有更大的代码示例是什么,但是看起来像numpyarray

If you have used import array there is a simple function that turns it back into a list and removed that header: new_list = new.tolist() 如果您使用了import array ,则有一个简单的函数将其转换为列表并删除该标头: new_list = new.tolist()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM