Python串联存储在列表中的不同大小的数组 [英] Python concatenating different size arrays stored in a list

查看:312
本文介绍了Python串联存储在列表中的不同大小的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有"Z"的列表:

I have a list 'Z' with:

import numpy as np
z[0] = np.random.normal( 0, 1, ( 500, 20 ) )
z[1] = np.random.normal( 0, 1, ( 500, 30 ) )

列表中大约有100个数组.我仅使用大小2列表进行说明.存储的数组的维数均为0,为500

There are about 100 arrays in the list. I am using only size 2 list for illustration. The stored arrays are all of dimension 0 of 500

我想要实现:

C = np.concatenate( ( z[0] , z[1] ),1)

我尝试过:

z1 = [ np.concatenate( z[ii], 1 ) for ii in range(0,len(z)) ] 

但是它仍然返回原始列表,并且不连接存储的数组

but it still returns the original list and doesn't concatenate the stored arrays

推荐答案

多维数组的级联定义不明确,但未指定用于级联的轴.我假设您想水平堆叠数组,因为两者的行数相同.最简单的调用是

Concatenation for multidimensional arrays is somewhat ill-defined without specifying an axis along which to concatenate. I assume you want to stack your arrays horizontally because the number of rows is the same for both. The simplest call is

stacked = np.hstack(Z)

将沿轴1连接.您可以找到文档

which will concatenate along axis 1. You can find documentation here.

更一般地,您也可以使用

More generally, you can also use

stacked = np.concatenate(Z, axis=1)

也适用于高维数组.相应的文档是此处.

which works for higher-dimensional arrays, too. The corresponding documentation is here.

这篇关于Python串联存储在列表中的不同大小的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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